Archived
1
This repository has been archived on 2022-08-08. You can view files and clone it. You cannot open issues or pull requests or push a commit.
Files
mathpad/MathKit/MPRootFunctionLayout.m
2017-08-31 00:04:21 +02:00

138 lines
3.9 KiB
Objective-C
Executable File

//
// MPRootFunctionLayout.m
// MathPad
//
// Created by Kim Wittenburg on 11.01.15.
// Copyright (c) 2015 Kim Wittenburg. All rights reserved.
//
#import "MPRootFunctionLayout.h"
#import "MPRootFunction.h"
#define kRootFunctionTrailingOffset 3
#define kRootFunctionLineWidth 1
#define kRootFunctionLineFragmentLength (self.usesSmallSize ? 3 : 4)
#define kRootFunctionVWidth 5
#define kRootFunctionExpressionXOffset 2
#define kRootFunctionExponentXInset kRootFunctionLineFragmentLength
@implementation MPRootFunctionLayout
- (MPRootFunction *)rootFunction
{
return (MPRootFunction *)self.function;
}
- (NSUInteger)indexOfLeadingChild
{
return 1;
}
- (NSUInteger)indexOfTrailingChild
{
return 1;
}
- (NSUInteger)indexOfChildBelowChildAtIndex:(NSUInteger)index
{
return 1;
}
- (NSUInteger)indexOfChildAboveChildAtIndex:(NSUInteger)index
{
return 0;
}
- (NSUInteger)indexOfChildAfterChildAtIndex:(NSUInteger)index
{
return index == 0 ? 1 : NSNotFound;
}
- (NSIndexSet *)indexesOfRemainingChildren
{
return [NSIndexSet indexSetWithIndexesInRange:NSMakeRange(0, 2)];
}
- (BOOL)childAtIndexUsesSmallSize:(NSUInteger)index
{
return index == 0;
}
- (NSPoint)offsetOfChildLayoutAtIndex:(NSUInteger)index
{
NSRect exponentBounds = [self childLayoutAtIndex:0].bounds;
NSRect rootBounds = [self childLayoutAtIndex:1].bounds;
if (index == 0) {
CGFloat x;
if (exponentBounds.size.width > kRootFunctionExponentXInset) {
x = 0;
} else {
x = kRootFunctionExponentXInset - exponentBounds.size.width;
}
return NSMakePoint(x, rootBounds.size.height / 2.0 + rootBounds.origin.y - exponentBounds.origin.y);
} else {
CGFloat rootSymbolXOffset = exponentBounds.size.width > kRootFunctionExponentXInset ? exponentBounds.size.width - kRootFunctionExponentXInset : 0;
CGFloat x = rootSymbolXOffset + kRootFunctionLineFragmentLength + kRootFunctionVWidth + kRootFunctionExpressionXOffset;
return NSMakePoint(x, 0);
}
}
- (NSIndexPath *)indexPathForLocalMousePoint:(NSPoint)point
{
return [[NSIndexPath indexPathWithIndex:1] indexPathByAddingIndex:0];
}
- (NSRect)generateBounds
{
CGFloat x, y, width, height;
NSRect exponentBounds = [self childLayoutAtIndex:0].bounds;
NSRect rootBounds = [self childLayoutAtIndex:1].bounds;
x = 0;
y = rootBounds.origin.y;
CGFloat rootSymbolXOffset = exponentBounds.size.width > kRootFunctionLineFragmentLength ? exponentBounds.size.width : kRootFunctionLineFragmentLength;
width = rootSymbolXOffset + kRootFunctionVWidth + kRootFunctionExpressionXOffset + rootBounds.size.width + kRootFunctionTrailingOffset;
CGFloat heightToExponent = rootBounds.size.height / 2.0 + exponentBounds.size.height;
height = MAX(heightToExponent, rootBounds.size.height + kRootFunctionLineWidth);
return NSMakeRect(x, y, width, height);
}
- (void)draw
{
NSRect exponentBounds = [self childLayoutAtIndex:0].bounds;
NSRect rootBounds = [self childLayoutAtIndex:1].bounds;
CGFloat rootSymbolXOffset = exponentBounds.size.width > kRootFunctionExponentXInset ? exponentBounds.size.width - kRootFunctionExponentXInset : 0;
NSBezierPath *rootPath = [NSBezierPath bezierPath];
NSPoint currentPoint = NSMakePoint(rootSymbolXOffset, rootBounds.size.height / 2.0 + rootBounds.origin.y);
[rootPath moveToPoint:currentPoint];
currentPoint.x += kRootFunctionLineFragmentLength;
[rootPath lineToPoint:currentPoint];
currentPoint.x += kRootFunctionVWidth / 2.0;
currentPoint.y = rootBounds.origin.y;
[rootPath lineToPoint:currentPoint];
currentPoint.x += kRootFunctionVWidth / 2.0;
currentPoint.y = rootBounds.size.height + rootBounds.origin.y;
[rootPath lineToPoint:currentPoint];
currentPoint.x += rootBounds.size.width;
[rootPath lineToPoint:currentPoint];
[rootPath stroke];
}
@end