mirror of
https://github.com/cappuccino/cappuccino.git
synced 2026-12-05 20:25:32 +00:00
new: UIBuilder demo application
This commit is contained in:
1 parent
570c31f248
commit
38f523b3c2
11 files changed
+3922
No files matched your search
@@ -0,0 +1,422 @@
|
||||
//
|
||||
// AppController.j
|
||||
// Main application controller. Sets up the window, canvas, palette,
|
||||
// and controllers on launch.
|
||||
//
|
||||
|
||||
@import <Foundation/CPObject.j>
|
||||
@import "UIBuilderController.j"
|
||||
@import "UICanvasView.j"
|
||||
@import "UIElementView.j";
|
||||
@import "InspectorController.j";
|
||||
|
||||
@implementation CPColor (StandardColors)
|
||||
|
||||
// A standard light gray for control backgrounds, like buttons.
|
||||
+ (CPColor)controlColor
|
||||
{
|
||||
return [CPColor colorWithCalibratedWhite:0.9 alpha:1.0];
|
||||
}
|
||||
|
||||
// A medium gray for shadows or borders.
|
||||
+ (CPColor)controlShadowColor
|
||||
{
|
||||
return [CPColor grayColor];
|
||||
}
|
||||
|
||||
// A dark gray for text on light controls.
|
||||
+ (CPColor)controlDarkShadowColor
|
||||
{
|
||||
return [CPColor darkGrayColor];
|
||||
}
|
||||
|
||||
// The primary color for selected items.
|
||||
+ (CPColor)selectedControlColor
|
||||
{
|
||||
// Corresponds to the default blue selection color in macOS.
|
||||
return [CPColor colorWithCalibratedRed:0.0 green:0.478 blue:1.0 alpha:1.0];
|
||||
}
|
||||
|
||||
// A secondary selection color, often used for inactive windows or rubber-band selections.
|
||||
+ (CPColor)alternateSelectedControlColor
|
||||
{
|
||||
return [CPColor colorWithCalibratedRed:0.2 green:0.5 blue:0.9 alpha:1.0];
|
||||
}
|
||||
|
||||
// The color for an inactive or secondary selection, like a window title bar.
|
||||
+ (CPColor)secondarySelectedControlColor
|
||||
{
|
||||
return [CPColor lightGrayColor];
|
||||
}
|
||||
|
||||
// The highlight color for an element that has keyboard focus.
|
||||
+ (CPColor)keyboardFocusIndicatorColor
|
||||
{
|
||||
return [CPColor colorWithCalibratedRed:0.3 green:0.6 blue:1.0 alpha:1.0];
|
||||
}
|
||||
|
||||
// The standard background color for a window's content area.
|
||||
+ (CPColor)windowBackgroundColor
|
||||
{
|
||||
return [CPColor colorWithCalibratedWhite:0.93 alpha:1.0];
|
||||
}
|
||||
|
||||
// The background color for text-editing views.
|
||||
+ (CPColor)textBackgroundColor
|
||||
{
|
||||
return [CPColor whiteColor];
|
||||
}
|
||||
|
||||
@end
|
||||
|
||||
// Required additions from original EFView.j for graphics and text handling
|
||||
@implementation CPString(SizingAddition)
|
||||
- (CPSize)sizeWithAttributes:(CPDictionary)stringAttributes
|
||||
{
|
||||
var font = [stringAttributes objectForKey:CPFontAttributeName] || [CPFont systemFontOfSize:12];
|
||||
// This is a simplified implementation. For more complex text, you might need a more robust solution.
|
||||
var ctx = [[CPGraphicsContext currentContext] graphicsPort];
|
||||
var oldFont = ctx.font;
|
||||
ctx.font = [font cssString];
|
||||
var metrics = ctx.measureText(self);
|
||||
ctx.font = oldFont;
|
||||
return CGSizeMake(metrics.width, [[font fontDescriptor] pointSize]);
|
||||
}
|
||||
- (void)drawAtPoint:(CGPoint)aPoint withAttributes:(CPDictionary)attributes
|
||||
{
|
||||
var ctx = [[CPGraphicsContext currentContext] graphicsPort];
|
||||
var font = [attributes objectForKey:CPFontAttributeName] || [CPFont systemFontOfSize:12];
|
||||
var color = [attributes objectForKey:CPForegroundColorAttributeName] || [CPColor blackColor];
|
||||
|
||||
ctx.font = [font cssString];
|
||||
[color setFill];
|
||||
ctx.fillText(self, aPoint.x, aPoint.y + [[font fontDescriptor] pointSize]);
|
||||
}
|
||||
@end
|
||||
|
||||
@implementation CPBezierPath(RoundedRectangle)
|
||||
+ (CPBezierPath)bezierPathWithRoundedRect:(CPRect)aRect radius:(float)radius
|
||||
{
|
||||
return [self bezierPathWithRoundedRect:aRect xRadius:radius yRadius:radius];
|
||||
}
|
||||
@end
|
||||
|
||||
|
||||
// A simple draggable symbol for the palette
|
||||
@implementation DraggableSymbolView : CPView
|
||||
{
|
||||
CPString _dragType;
|
||||
}
|
||||
|
||||
- (void)setDragType:(CPString)aType
|
||||
{
|
||||
_dragType = aType;
|
||||
}
|
||||
-(BOOL)acceptsFirstMouse:(CPEvent)aEvent
|
||||
{
|
||||
return YES;
|
||||
}
|
||||
|
||||
- (void)mouseDown:(CPEvent)theEvent
|
||||
{
|
||||
// 1. Create a placeholder view that is a visual copy of this one.
|
||||
var dragPlaceholder = [[DraggableSymbolView alloc] initWithFrame:[self bounds]];
|
||||
[dragPlaceholder setDragType:_dragType]; // Ensure it can draw its title correctly
|
||||
[dragPlaceholder setAlphaValue:0.75]; // Make it semi-transparent for good UX
|
||||
|
||||
var pasteboard = [CPPasteboard pasteboardWithName:CPDragPboard];
|
||||
[pasteboard declareTypes:[_dragType] owner:nil];
|
||||
[pasteboard setString:@"1" forType:_dragType];
|
||||
|
||||
[self dragView:dragPlaceholder
|
||||
at:[self bounds].origin
|
||||
offset:nil
|
||||
event:theEvent
|
||||
pasteboard:pasteboard
|
||||
source:self
|
||||
slideBack:YES];
|
||||
}
|
||||
|
||||
// The drawRect: method defines what the view looks like, and therefore
|
||||
// what the dragged placeholder view will look like.
|
||||
- (void)drawRect:(CGRect)rect
|
||||
{
|
||||
var bounds = [self bounds];
|
||||
|
||||
// Background
|
||||
[[CPColor controlColor] set];
|
||||
[CPBezierPath fillRect:bounds];
|
||||
[[CPColor controlShadowColor] set];
|
||||
[CPBezierPath strokeRect:bounds];
|
||||
|
||||
if ([_dragType isEqualToString:UIWindowDragType])
|
||||
{
|
||||
// Draw a window
|
||||
var windowRect = CGRectInset(bounds, 5, 5);
|
||||
var titleBarHeight = 10;
|
||||
|
||||
// Draw the title bar
|
||||
var titleBarRect = CGRectMake(windowRect.origin.x, windowRect.origin.y, windowRect.size.width, titleBarHeight);
|
||||
[[CPColor grayColor] set];
|
||||
[CPBezierPath fillRect:titleBarRect];
|
||||
|
||||
// Draw the content area
|
||||
var contentRect = CGRectMake(windowRect.origin.x, windowRect.origin.y + titleBarHeight, windowRect.size.width, windowRect.size.height - titleBarHeight);
|
||||
[[CPColor whiteColor] set];
|
||||
[CPBezierPath fillRect:contentRect];
|
||||
|
||||
// Draw the border for the whole window
|
||||
[[CPColor blackColor] set];
|
||||
[CPBezierPath strokeRect:windowRect];
|
||||
}
|
||||
else if ([_dragType isEqualToString:UIButtonDragType])
|
||||
{
|
||||
// Draw a button
|
||||
var buttonRect = CGRectInset(bounds, 8, 10);
|
||||
var path = [CPBezierPath bezierPathWithRoundedRect:buttonRect radius:5];
|
||||
[[CPColor whiteColor] set];
|
||||
[path fill];
|
||||
[[CPColor blackColor] set];
|
||||
[path stroke];
|
||||
}
|
||||
else if ([_dragType isEqualToString:UISliderDragType])
|
||||
{
|
||||
// Draw a slider
|
||||
var sliderY = bounds.size.height / 2;
|
||||
var path = [CPBezierPath bezierPath];
|
||||
[path moveToPoint:CGPointMake(bounds.origin.x + 5, sliderY)];
|
||||
[path lineToPoint:CGPointMake(bounds.origin.x + bounds.size.width - 5, sliderY)];
|
||||
[[CPColor blackColor] set];
|
||||
[path stroke];
|
||||
|
||||
var knobRect = CGRectMake(bounds.size.width / 2 - 5, sliderY - 5, 10, 10);
|
||||
var knobPath = [CPBezierPath bezierPathWithOvalInRect:knobRect];
|
||||
[[CPColor whiteColor] set];
|
||||
[knobPath fill];
|
||||
[[CPColor blackColor] set];
|
||||
[knobPath stroke];
|
||||
}
|
||||
else if ([_dragType isEqualToString:UITextFieldDragType])
|
||||
{
|
||||
// Draw a text field
|
||||
var fieldRect = CGRectInset(bounds, 5, 12);
|
||||
[[CPColor whiteColor] set];
|
||||
[CPBezierPath fillRect:fieldRect];
|
||||
[[CPColor blackColor] set];
|
||||
[CPBezierPath strokeRect:fieldRect];
|
||||
|
||||
// Draw an I-beam cursor
|
||||
var ibeamX = CGRectGetMidX(fieldRect);
|
||||
var ibeamY1 = CGRectGetMinY(fieldRect) + 3;
|
||||
var ibeamY2 = CGRectGetMaxY(fieldRect) - 3;
|
||||
|
||||
var ibeamPath = [CPBezierPath bezierPath];
|
||||
[ibeamPath moveToPoint:CGPointMake(ibeamX, ibeamY1)];
|
||||
[ibeamPath lineToPoint:CGPointMake(ibeamX, ibeamY2)];
|
||||
[ibeamPath moveToPoint:CGPointMake(ibeamX - 2, ibeamY1)];
|
||||
[ibeamPath lineToPoint:CGPointMake(ibeamX + 2, ibeamY1)];
|
||||
[ibeamPath moveToPoint:CGPointMake(ibeamX - 2, ibeamY2)];
|
||||
[ibeamPath lineToPoint:CGPointMake(ibeamX + 2, ibeamY2)];
|
||||
|
||||
[ibeamPath setLineWidth:0.5];
|
||||
[[CPColor blackColor] set];
|
||||
[ibeamPath stroke];
|
||||
}
|
||||
else
|
||||
{
|
||||
// Fallback to original text drawing
|
||||
var title = [[_dragType componentsSeparatedByString:@"DragType"] objectAtIndex:0];
|
||||
var textAttributes = @{
|
||||
CPFontAttributeName: [CPFont systemFontOfSize:10],
|
||||
CPForegroundColorAttributeName: [CPColor blackColor]
|
||||
};
|
||||
var titleSize = [title sizeWithAttributes:textAttributes];
|
||||
var titlePoint = CGPointMake(
|
||||
(bounds.size.width - titleSize.width) / 2.0,
|
||||
(bounds.size.height - titleSize.height) / 2.0
|
||||
);
|
||||
[title drawAtPoint:titlePoint withAttributes:textAttributes];
|
||||
}
|
||||
}
|
||||
|
||||
@end
|
||||
|
||||
@implementation AppController : CPObject
|
||||
{
|
||||
CPWindow _window;
|
||||
CPPanel _palette;
|
||||
UIBuilderController _builderController;
|
||||
UICanvasView _canvasView;
|
||||
InspectorController _inspectorController;
|
||||
}
|
||||
|
||||
- (void)applicationDidFinishLaunching:(CPNotification)aNotification
|
||||
{
|
||||
// 1. Create the main window and canvas
|
||||
_window = [[CPWindow alloc] initWithContentRect:CGRectMakeZero() styleMask:CPBorderlessBridgeWindowMask];
|
||||
[_window setTitle:@"Cappuccino UI Builder"];
|
||||
[_window setAcceptsMouseMovedEvents:YES];
|
||||
|
||||
_canvasView = [[UICanvasView alloc] initWithFrame:[[_window contentView] bounds]];
|
||||
[_canvasView setAutoresizingMask:CPViewWidthSizable | CPViewHeightSizable];
|
||||
[[_window contentView] addSubview:_canvasView];
|
||||
|
||||
// 2. Create the controllers
|
||||
_builderController = [[UIBuilderController alloc] init];
|
||||
|
||||
// 3. Wire everything together
|
||||
[_canvasView setDelegate:_builderController];
|
||||
|
||||
// Bind the canvas to the controller's data model. This is the core of the architecture.
|
||||
[_canvasView bind:"dataObjects" toObject:_builderController withKeyPath:@"elementsController.arrangedObjects" options:nil];
|
||||
[_canvasView bind:"selectionIndexes" toObject:_builderController withKeyPath:@"elementsController.selectionIndexes" options:nil];
|
||||
[_canvasView bind:"connections" toObject:_builderController withKeyPath:@"connectionsController.arrangedObjects" options:nil];
|
||||
[_canvasView bind:"selectedConnections" toObject:_builderController withKeyPath:@"connectionsController.selectedObjects" options:nil];
|
||||
|
||||
[self createPalette];
|
||||
[self createInspector];
|
||||
|
||||
// 5. Create the main menu
|
||||
var mainMenuBar = [[CPMenu alloc] initWithTitle:@"MainMenu"];
|
||||
var editMenuItem = [[CPMenuItem alloc] initWithTitle:@"Edit" action:nil keyEquivalent:@""];
|
||||
|
||||
|
||||
var editMenu = [[CPMenu alloc] initWithTitle:@"Edit"];
|
||||
[editMenu addItemWithTitle:@"Undo" action:@selector(undo:) keyEquivalent:@"z"];
|
||||
[editMenu addItemWithTitle:@"Redo" action:@selector(redo:) keyEquivalent:@"Z"];
|
||||
[editMenu addItem:[CPMenuItem separatorItem]];
|
||||
[editMenu addItemWithTitle:@"Cut" action:@selector(cut:) keyEquivalent:@"x"];
|
||||
[editMenu addItemWithTitle:@"Copy" action:@selector(copy:) keyEquivalent:@"c"];
|
||||
[editMenu addItemWithTitle:@"Paste" action:@selector(paste:) keyEquivalent:@"v"];
|
||||
[editMenu addItemWithTitle:@"Delete" action:@selector(delete:) keyEquivalent:@""];
|
||||
|
||||
[editMenuItem setSubmenu:editMenu];
|
||||
|
||||
var fileMenuItem = [[CPMenuItem alloc] initWithTitle:@"File" action:nil keyEquivalent:@""];
|
||||
var fileMenu = [[CPMenu alloc] initWithTitle:@"File"];
|
||||
[fileMenu addItemWithTitle:@"Run" action:@selector(run:) keyEquivalent:@"r"];
|
||||
[fileMenuItem setSubmenu:fileMenu];
|
||||
[mainMenuBar addItem:fileMenuItem];
|
||||
|
||||
[mainMenuBar addItem:editMenuItem];
|
||||
|
||||
[CPApp setMainMenu:mainMenuBar];
|
||||
[CPMenu setMenuBarVisible:YES];
|
||||
|
||||
[_window makeKeyAndOrderFront:self];
|
||||
}
|
||||
|
||||
- (void)createPalette
|
||||
{
|
||||
var screenWidth = window.innerWidth;
|
||||
var paletteWidth = 220;
|
||||
var paletteHeight = 60;
|
||||
var paletteX = (screenWidth - paletteWidth) / 2;
|
||||
var paletteY = 22; // Position near the top of the screen
|
||||
|
||||
_palette = [[CPPanel alloc] initWithContentRect:CGRectMake(paletteX, paletteY, paletteWidth, paletteHeight)
|
||||
styleMask:CPHUDBackgroundWindowMask | CPTitledWindowMask | CPClosableWindowMask];
|
||||
[_palette setTitle:@"Elements"];
|
||||
[_palette setFloatingPanel:YES];
|
||||
|
||||
var xPos = 10;
|
||||
var types = [UIWindowDragType, UIButtonDragType, UISliderDragType, UITextFieldDragType];
|
||||
|
||||
// Create draggable symbols for each type
|
||||
[_canvasView registerForDraggedTypes:types];
|
||||
|
||||
for (var i=0; i < [types count]; i++) {
|
||||
var symbol = [[DraggableSymbolView alloc] initWithFrame:CGRectMake(xPos, 10, 40, 40)];
|
||||
symbol._dragType = types[i];
|
||||
|
||||
[[_palette contentView] addSubview:symbol];
|
||||
xPos += 50;
|
||||
}
|
||||
|
||||
[_palette orderFront:self];
|
||||
}
|
||||
|
||||
- (void)createInspector
|
||||
{
|
||||
var inspectorPanel = [[CPPanel alloc] initWithContentRect:CGRectMake(20, 200, 300, 150)
|
||||
styleMask:CPTitledWindowMask | CPClosableWindowMask];
|
||||
[inspectorPanel setTitle:@"Inspector"];
|
||||
[inspectorPanel setFloatingPanel:YES];
|
||||
|
||||
var contentView = [inspectorPanel contentView];
|
||||
|
||||
_inspectorController = [[InspectorController alloc] init];
|
||||
[_inspectorController setBuilderController:_builderController];
|
||||
[_inspectorController setPanel:inspectorPanel];
|
||||
[_inspectorController setView:contentView];
|
||||
|
||||
[_inspectorController awakeFromMarkup]; // Manually call this
|
||||
|
||||
[inspectorPanel orderFront:self];
|
||||
}
|
||||
|
||||
- (void)run:(id)sender
|
||||
{
|
||||
console.log("Run: Starting native UI generation...");
|
||||
var canvasSubviews = [_canvasView subviews];
|
||||
var nativeElementMap = [CPMutableDictionary dictionary];
|
||||
|
||||
// First pass: create all native elements and map them by their ID
|
||||
console.log("Run: Creating native elements and building map...");
|
||||
for (var i = 0; i < [canvasSubviews count]; i++)
|
||||
{
|
||||
var view = [canvasSubviews objectAtIndex:i];
|
||||
if ([view isKindOfClass:[UIElementView class]])
|
||||
{
|
||||
// This will now recursively build the map
|
||||
[view nativeUIElementWithMap:nativeElementMap];
|
||||
}
|
||||
}
|
||||
|
||||
// Second pass: connect the native elements
|
||||
console.log("Run: Processing connections...");
|
||||
var connections = [[_builderController connectionsController] content];
|
||||
for (var i = 0; i < [connections count]; i++)
|
||||
{
|
||||
var connection = [connections objectAtIndex:i];
|
||||
var sourceID = [connection valueForKey:@"sourceID"];
|
||||
var targetID = [connection valueForKey:@"targetID"];
|
||||
var action = [connection valueForKey:@"action"];
|
||||
|
||||
console.log(" - Connecting: " + sourceID + " -> " + targetID + " (Action: " + action + ")");
|
||||
|
||||
var nativeSource = [nativeElementMap objectForKey:sourceID];
|
||||
var nativeTarget = [nativeElementMap objectForKey:targetID];
|
||||
|
||||
if (nativeSource && nativeTarget && action)
|
||||
{
|
||||
console.log(" - Found native source and target. Applying connection.");
|
||||
[nativeSource setTarget:nativeTarget];
|
||||
[nativeSource setAction:CPSelectorFromString(action)];
|
||||
}
|
||||
else
|
||||
{
|
||||
console.log(" - WARNING: Could not find native source or target for connection.");
|
||||
}
|
||||
}
|
||||
|
||||
// Third pass: show the windows
|
||||
console.log("Run: Showing windows...");
|
||||
for (var i = 0; i < [canvasSubviews count]; i++)
|
||||
{
|
||||
var view = [canvasSubviews objectAtIndex:i];
|
||||
if ([view isKindOfClass:[UIWindowView class]])
|
||||
{
|
||||
var elementID = [[view dataObject] valueForKey:@"id"];
|
||||
var nativeWindow = [nativeElementMap objectForKey:elementID];
|
||||
if (nativeWindow)
|
||||
{
|
||||
console.log(" - Showing window for ID: " + elementID);
|
||||
[nativeWindow makeKeyAndOrderFront:self];
|
||||
}
|
||||
}
|
||||
}
|
||||
console.log("Run: Finished.");
|
||||
}
|
||||
|
||||
@end
|
||||
@@ -0,0 +1,97 @@
|
||||
|
||||
@import <Foundation/CPObject.j>
|
||||
|
||||
function treshold(value, limit)
|
||||
{
|
||||
return value > 0 ? Math.min(value, limit) : Math.max(value, -limit);
|
||||
}
|
||||
|
||||
@implementation ConnectionView : CPView
|
||||
{
|
||||
CGPoint _startPoint;
|
||||
CGPoint _endPoint;
|
||||
CPColor _color;
|
||||
}
|
||||
|
||||
- (id)initWithFrame:(CGRect)aRect
|
||||
{
|
||||
self = [super initWithFrame:aRect];
|
||||
if (self)
|
||||
{
|
||||
[self setBackgroundColor:[CPColor clearColor]];
|
||||
_color = [CPColor redColor];
|
||||
[self setHidden:YES]; // Hidden by default
|
||||
}
|
||||
return self;
|
||||
}
|
||||
|
||||
- (void)setStartPoint:(CGPoint)startPoint { _startPoint = startPoint; }
|
||||
- (void)setEndPoint:(CGPoint)endPoint { _endPoint = endPoint; }
|
||||
- (void)setColor:(CPColor)color { _color = color; }
|
||||
|
||||
- (void)drawRect:(CGRect)rect
|
||||
{
|
||||
console.log("ConnectionView: drawRect - Drawing connection from ", _startPoint, " to ", _endPoint);
|
||||
if (_startPoint && _endPoint)
|
||||
{
|
||||
[self drawLinkFrom:_startPoint to:_endPoint color:_color];
|
||||
}
|
||||
}
|
||||
|
||||
- (void)drawLinkFrom:(CGPoint)startPoint to:(CGPoint)endPoint color:(CPColor)insideColor
|
||||
{
|
||||
var dist = Math.sqrt(Math.pow(startPoint.x - endPoint.x, 2) + Math.pow(startPoint.y - endPoint.y, 2));
|
||||
var p0 = CGPointMake(startPoint.x, startPoint.y);
|
||||
var p3 = CGPointMake(endPoint.x, endPoint.y);
|
||||
var p1 = CGPointMake(startPoint.x + treshold((endPoint.x - startPoint.x) / 2, 50), startPoint.y);
|
||||
var p2 = CGPointMake(endPoint.x - treshold((endPoint.x - startPoint.x) / 2, 50), endPoint.y);
|
||||
var path = [CPBezierPath bezierPath];
|
||||
[path setLineWidth:0];
|
||||
[[CPColor grayColor] set];
|
||||
[path appendBezierPathWithOvalInRect:CGRectMake(startPoint.x-2.5,startPoint.y-2.5,5,5)];
|
||||
[path fill];
|
||||
path = [CPBezierPath bezierPath];
|
||||
[path setLineWidth:0];
|
||||
[insideColor set];
|
||||
[path appendBezierPathWithOvalInRect:CGRectMake(startPoint.x-1.5,startPoint.y-1.5,3,3)];
|
||||
[path fill];
|
||||
path = [CPBezierPath bezierPath];
|
||||
[path setLineWidth:0];
|
||||
[[CPColor grayColor] set];
|
||||
[path appendBezierPathWithOvalInRect:CGRectMake(endPoint.x-2.5,endPoint.y-2.5,5,5)];
|
||||
[path fill];
|
||||
path = [CPBezierPath bezierPath];
|
||||
[path setLineWidth:0];
|
||||
[insideColor set];
|
||||
[path appendBezierPathWithOvalInRect:CGRectMake(endPoint.x-1.5,endPoint.y-1.5,3,3)];
|
||||
[path fill];
|
||||
if (dist < 40)
|
||||
{
|
||||
path = [CPBezierPath bezierPath];
|
||||
[path setLineWidth:5];
|
||||
[path moveToPoint:startPoint];
|
||||
[path lineToPoint:endPoint];
|
||||
[[CPColor grayColor] set];
|
||||
[path stroke];
|
||||
path = [CPBezierPath bezierPath];
|
||||
[path setLineWidth:3];
|
||||
[path moveToPoint:startPoint];
|
||||
[path lineToPoint:endPoint];
|
||||
[insideColor set];
|
||||
[path stroke];
|
||||
return;
|
||||
}
|
||||
path = [CPBezierPath bezierPath];
|
||||
[path setLineWidth:5];
|
||||
[path moveToPoint:p0];
|
||||
[path curveToPoint:p3 controlPoint1:p1 controlPoint2:p2];
|
||||
[[CPColor grayColor] set];
|
||||
[path stroke];
|
||||
path = [CPBezierPath bezierPath];
|
||||
[path setLineWidth:3];
|
||||
[path moveToPoint:p0];
|
||||
[path curveToPoint:p3 controlPoint1:p1 controlPoint2:p2];
|
||||
[insideColor set];
|
||||
[path stroke];
|
||||
}
|
||||
@end
|
||||
Executable
+14
@@ -0,0 +1,14 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
|
||||
<plist version="1.0">
|
||||
<dict>
|
||||
<key>CPApplicationDelegateClass</key>
|
||||
<string>AppController</string>
|
||||
<key>CPBundleName</key>
|
||||
<string>UIBuilderDemo</string>
|
||||
<key>CPPrincipalClass</key>
|
||||
<string>CPApplication</string>
|
||||
<key>CPDefaultTheme</key>
|
||||
<string>Aristo2</string>
|
||||
</dict>
|
||||
</plist>
|
||||
@@ -0,0 +1,215 @@
|
||||
@import <AppKit/CPViewController.j>
|
||||
|
||||
@class UIBuilderController;
|
||||
|
||||
@implementation InspectorController : CPViewController
|
||||
{
|
||||
UIBuilderController _builderController @accessors(property=builderController);
|
||||
CPPanel _panel @accessors(property=panel);
|
||||
CPTableView _connectionsTableView;
|
||||
}
|
||||
|
||||
- (void)awakeFromMarkup
|
||||
{
|
||||
[_builderController addObserver:self forKeyPath:@"elementsController.selectionIndexes" options:CPKeyValueObservingOptionNew context:nil];
|
||||
|
||||
// Create Tab View
|
||||
var tabView = [[CPTabView alloc] initWithFrame:[[_panel contentView] bounds]];
|
||||
[tabView setAutoresizingMask:CPViewWidthSizable | CPViewHeightSizable];
|
||||
[tabView setDelegate:self];
|
||||
|
||||
// Properties Tab
|
||||
var propertiesView = [[CPView alloc] initWithFrame:CGRectMakeZero()];
|
||||
var propertiesTabItem = [[CPTabViewItem alloc] initWithIdentifier:@"properties"];
|
||||
[propertiesTabItem setLabel:@"Properties"];
|
||||
[propertiesTabItem setView:propertiesView];
|
||||
[tabView addTabViewItem:propertiesTabItem];
|
||||
|
||||
// Connections Tab
|
||||
var connectionsView = [[CPView alloc] initWithFrame:CGRectMakeZero()];
|
||||
var connectionsTabItem = [[CPTabViewItem alloc] initWithIdentifier:@"connections"];
|
||||
[connectionsTabItem setLabel:@"Connections"];
|
||||
[connectionsTabItem setView:connectionsView];
|
||||
[tabView addTabViewItem:connectionsTabItem];
|
||||
|
||||
// Connections TableView
|
||||
_connectionsTableView = [[CPTableView alloc] initWithFrame:[connectionsView bounds]];
|
||||
[_connectionsTableView setAutoresizingMask:CPViewWidthSizable | CPViewHeightSizable];
|
||||
|
||||
var columns = [
|
||||
{identifier: "outlet", title: "Outlet", width: 80},
|
||||
{identifier: "action", title: "Action", width: 120}
|
||||
];
|
||||
|
||||
// Keep a reference to the array controller
|
||||
var connectionsController = [_builderController connectionsController];
|
||||
|
||||
for (var i = 0; i < [columns count]; i++) {
|
||||
var colInfo = columns[i];
|
||||
var column = [[CPTableColumn alloc] initWithIdentifier:colInfo.identifier];
|
||||
[[column headerView] setStringValue:colInfo.title];
|
||||
[column setWidth:colInfo.width];
|
||||
[_connectionsTableView addTableColumn:column];
|
||||
// Bind the value of each column to the corresponding key path on the arranged objects
|
||||
[column bind:CPValueBinding toObject:connectionsController withKeyPath:("arrangedObjects." + colInfo.identifier) options:nil];
|
||||
}
|
||||
|
||||
// Bind the table's selection to the array controller's selection
|
||||
[_connectionsTableView bind:@"selectionIndexes" toObject:connectionsController withKeyPath:@"selectionIndexes" options:nil];
|
||||
|
||||
var connectionsViewBounds = [connectionsView bounds];
|
||||
var buttonBarHeight = 28;
|
||||
var tableHeight = connectionsViewBounds.size.height - buttonBarHeight;
|
||||
|
||||
var scrollViewFrame = CGRectMake(3, 3, connectionsViewBounds.size.width - 6, tableHeight - 6);
|
||||
var buttonBarFrame = CGRectMake(0, tableHeight, connectionsViewBounds.size.width, buttonBarHeight);
|
||||
|
||||
var scrollView = [[CPScrollView alloc] initWithFrame:scrollViewFrame];
|
||||
[scrollView setAutoresizingMask:CPViewWidthSizable | CPViewHeightSizable];
|
||||
[scrollView setDocumentView:_connectionsTableView];
|
||||
[connectionsView addSubview:scrollView];
|
||||
|
||||
var buttonBar = [[CPView alloc] initWithFrame:buttonBarFrame];
|
||||
[buttonBar setAutoresizingMask:CPViewWidthSizable | CPViewMinYMargin]; // Stick to bottom
|
||||
[connectionsView addSubview:buttonBar];
|
||||
|
||||
var deleteButton = [CPButtonBar minusButton];
|
||||
[deleteButton setAction:@selector(deleteSelectedConnection:)];
|
||||
[deleteButton setTarget:self];
|
||||
[buttonBar addSubview:deleteButton];
|
||||
|
||||
// Replace panel's content view with the tab view
|
||||
[_panel setContentView:tabView];
|
||||
|
||||
[self updateInspector];
|
||||
}
|
||||
|
||||
- (id)tableView:(CPTableView)aTableView objectValueForTableColumn:(CPTableColumn)aTableColumn row:(int)aRow
|
||||
{
|
||||
var connection = [[[_builderController connectionsController] arrangedObjects] objectAtIndex:aRow];
|
||||
var identifier = [aTableColumn identifier];
|
||||
|
||||
return [connection valueForKey:identifier];
|
||||
}
|
||||
|
||||
- (void)observeValueForKeyPath:(CPString)keyPath ofObject:(id)object change:(CPDictionary)change context:(id)context
|
||||
{
|
||||
// We only observe selection changes now.
|
||||
if (keyPath === @"elementsController.selectionIndexes")
|
||||
{
|
||||
[self updateInspector];
|
||||
[self _updateConnectionVisibility];
|
||||
}
|
||||
}
|
||||
|
||||
- (void)_updateConnectionVisibility
|
||||
{
|
||||
var tabView = [[self panel] contentView];
|
||||
if (![tabView isKindOfClass:[CPTabView class]])
|
||||
return;
|
||||
|
||||
var selectedTabViewItem = [tabView selectedTabViewItem];
|
||||
var connectionsController = [_builderController connectionsController];
|
||||
var selectedObjects = [[_builderController elementsController] selectedObjects];
|
||||
|
||||
// 1. Filter the connections based on the selected UI element.
|
||||
if ([selectedObjects count] === 1)
|
||||
{
|
||||
var selectedID = [[selectedObjects objectAtIndex:0] valueForKey:@"id"];
|
||||
var predicate = [CPPredicate predicateWithFormat:@"sourceID == %@ OR targetID == %@", selectedID, selectedID];
|
||||
[connectionsController setFilterPredicate:predicate];
|
||||
}
|
||||
else
|
||||
{
|
||||
[connectionsController setFilterPredicate:[CPPredicate predicateWithFormat:@"FALSEPREDICATE"]];
|
||||
}
|
||||
}
|
||||
|
||||
- (void)tabView:(CPTabView)aTabView didSelectTabViewItem:(CPTabViewItem)aTabViewItem
|
||||
{
|
||||
[self _updateConnectionVisibility];
|
||||
}
|
||||
|
||||
- (void)deleteSelectedConnection:(id)sender
|
||||
{
|
||||
var selectedObjects = [[_builderController connectionsController] selectedObjects];
|
||||
if ([selectedObjects count] > 0)
|
||||
[[_builderController connectionsController] removeObjects:selectedObjects];
|
||||
}
|
||||
|
||||
- (void)updateInspector
|
||||
{
|
||||
var selectedObjects = [[_builderController elementsController] selectedObjects];
|
||||
var propertiesView = [[[_panel contentView] tabViewItemAtIndex:0] view];
|
||||
|
||||
// Clear existing views from properties tab
|
||||
var subviews = [propertiesView subviews];
|
||||
for (var i = [subviews count] - 1; i >= 0; i--) {
|
||||
[subviews[i] removeFromSuperview];
|
||||
}
|
||||
|
||||
if ([selectedObjects count] === 1)
|
||||
{
|
||||
var selectedObject = selectedObjects[0];
|
||||
var elementType = [selectedObject valueForKey:@"type"];
|
||||
var viewClass = [UIBuilderController classForElementType:elementType];
|
||||
var properties = [viewClass persistentProperties];
|
||||
|
||||
var yPos = 10;
|
||||
|
||||
// Set panel title
|
||||
[_panel setTitle:elementType];
|
||||
|
||||
for (var i = 0; i < [properties count]; i++)
|
||||
{
|
||||
var propertyName = properties[i];
|
||||
var value = [selectedObject valueForKey:propertyName];
|
||||
var propertyType = [[viewClass propertyTypes] valueForKey:propertyName];
|
||||
|
||||
// Create Label
|
||||
var label = [[CPTextField alloc] initWithFrame:CGRectMake(10, yPos + 3, 100, 20)];
|
||||
[label setStringValue:propertyName];
|
||||
[label setBezeled:NO];
|
||||
[label setDrawsBackground:NO];
|
||||
[label setEditable:NO];
|
||||
[propertiesView addSubview:label];
|
||||
[label setTextColor:[CPColor grayColor]];
|
||||
|
||||
// Create Control based on property type
|
||||
if (propertyType === UIBBoolean) {
|
||||
var checkbox = [[CPCheckBox alloc] initWithFrame:CGRectMake(120, yPos, 100, 20)];
|
||||
[checkbox setTitle:@""];
|
||||
[checkbox bind:@"value" toObject:selectedObject withKeyPath:propertyName options:nil];
|
||||
[propertiesView addSubview:checkbox];
|
||||
} else if (propertyType === UIBString || propertyType === UIBNumber) {
|
||||
var textField = [[CPTextField alloc] initWithFrame:CGRectMake(120, yPos, 150, 27)];
|
||||
[textField bind:@"value" toObject:selectedObject withKeyPath:propertyName options:nil];
|
||||
[textField setBezeled:YES];
|
||||
[textField setEditable:YES];
|
||||
[propertiesView addSubview:textField];
|
||||
} else { // Fallback for unknown types
|
||||
var textField = [[CPTextField alloc] initWithFrame:CGRectMake(120, yPos, 150, 25)];
|
||||
[textField bind:@"value" toObject:selectedObject withKeyPath:propertyName options:nil];
|
||||
[textField setBezeled:YES];
|
||||
[textField setEditable:YES];
|
||||
[propertiesView addSubview:textField];
|
||||
}
|
||||
|
||||
yPos += 30;
|
||||
}
|
||||
|
||||
[[self panel] orderFront:self];
|
||||
}
|
||||
else
|
||||
{
|
||||
[[self panel] orderOut:self];
|
||||
}
|
||||
}
|
||||
|
||||
- (void)dealloc
|
||||
{
|
||||
[_builderController removeObserver:self forKeyPath:@"elementsController.selectionIndexes"];
|
||||
[super dealloc];
|
||||
}
|
||||
|
||||
@end
|
||||
BIN
Binary file not shown.
|
After Width: | Height: | Size: 1.8 KiB |
@@ -0,0 +1,10 @@
|
||||
//
|
||||
// UIBuilderConstants.j
|
||||
// Defines global constants for the UI Builder application.
|
||||
//
|
||||
|
||||
UIBuilderElementPboardType = "UIBuilderElementPboardType";
|
||||
UIWindowDragType = "UIWindowDragType";
|
||||
UIButtonDragType = "UIButtonDragType";
|
||||
UISliderDragType = "UISliderDragType";
|
||||
UITextFieldDragType = "UITextFieldDragType";
|
||||
@@ -0,0 +1,645 @@
|
||||
//
|
||||
// UIBuilderController.j
|
||||
// This is the main controller for the UI Builder application.
|
||||
// It manages the data model for all elements on the canvas and acts
|
||||
// as a delegate for the UICanvasView to respond to user interactions.
|
||||
//
|
||||
// By Daniel Boehringer in 2025.
|
||||
//
|
||||
|
||||
@import <Foundation/CPObject.j>
|
||||
@import "UIElementView.j"
|
||||
@import "UICanvasView.j"
|
||||
@import "UIBuilderConstants.j";
|
||||
|
||||
// This is a simple data model. In a real app, it might have more properties.
|
||||
// We use a custom dictionary to ensure KVO compatibility and proper value setting.
|
||||
@implementation CPConservativeDictionary : CPDictionary
|
||||
{ }
|
||||
|
||||
- (id)init
|
||||
{
|
||||
self = [super init];
|
||||
if (self) {
|
||||
// Rely on superclass to initialize _buckets
|
||||
}
|
||||
return self;
|
||||
}
|
||||
|
||||
+ (id)dictionary
|
||||
{
|
||||
return [[self alloc] init];
|
||||
}
|
||||
|
||||
- (void)setValue:(id)aVal forKey:(CPString)aKey
|
||||
{
|
||||
// Only set the value if it's different from the current value
|
||||
var currentValue = [super valueForKey:aKey];
|
||||
|
||||
|
||||
// Always set the value if the current value is null or undefined
|
||||
if (currentValue == null || currentValue == undefined || currentValue != aVal) {
|
||||
[super setValue:aVal forKey:aKey];
|
||||
}
|
||||
}
|
||||
|
||||
- (BOOL)isEqual:(id)otherObject
|
||||
{
|
||||
return [self valueForKey:'id'] == [otherObject valueForKey:'id'];
|
||||
}
|
||||
|
||||
- (id)initWithCoder:(CPCoder)aCoder
|
||||
{
|
||||
self = [super initWithCoder:aCoder];
|
||||
if (self)
|
||||
{
|
||||
var allKeys = [aCoder decodeObjectForKey:@"CPConservativeDictionaryKeys"];
|
||||
if (allKeys)
|
||||
{
|
||||
for (var i = 0; i < [allKeys count]; i++)
|
||||
{
|
||||
var key = allKeys[i];
|
||||
var value = [aCoder decodeObjectForKey:key];
|
||||
[self setObject:value forKey:key];
|
||||
}
|
||||
}
|
||||
}
|
||||
return self;
|
||||
}
|
||||
|
||||
- (void)encodeWithCoder:(CPCoder)aCoder
|
||||
{
|
||||
[super encodeWithCoder:aCoder];
|
||||
var allKeys = [self allKeys];
|
||||
[aCoder encodeObject:allKeys forKey:@"CPConservativeDictionaryKeys"];
|
||||
for (var i = 0; i < [allKeys count]; i++)
|
||||
{
|
||||
var key = allKeys[i];
|
||||
[aCoder encodeObject:[self objectForKey:key] forKey:key];
|
||||
}
|
||||
}
|
||||
|
||||
@end
|
||||
|
||||
|
||||
@implementation UIBuilderController : CPViewController
|
||||
{
|
||||
CPArrayController _elementsController @accessors(property=elementsController);
|
||||
CPArrayController _connectionsController @accessors(property=connectionsController);
|
||||
CPMutableArray _connections;
|
||||
int _elementCounter; // To generate unique IDs
|
||||
}
|
||||
|
||||
+ (Class)classForElementType:(CPString)elementType
|
||||
{
|
||||
if (elementType === "window") return UIWindowView;
|
||||
if (elementType === "button") return UIButtonView;
|
||||
if (elementType === "slider") return UISliderView;
|
||||
if (elementType === "textfield") return UITextFieldView;
|
||||
return UIElementView;
|
||||
}
|
||||
|
||||
- (id)init
|
||||
{
|
||||
self = [super init];
|
||||
if (self) {
|
||||
_elementsController = [[CPArrayController alloc] init];
|
||||
_connectionsController = [[CPArrayController alloc] init];
|
||||
_elementCounter = 0;
|
||||
}
|
||||
return self;
|
||||
}
|
||||
|
||||
#pragma mark -
|
||||
#pragma mark Data Management
|
||||
|
||||
- (CPDictionary)_containerDataAtPoint:(CGPoint)aPoint
|
||||
{
|
||||
var allElements = [_elementsController arrangedObjects];
|
||||
for (var i = [allElements count] - 1; i >= 0; i--)
|
||||
{
|
||||
var elementData = allElements[i];
|
||||
var type = [elementData valueForKey:@"type"];
|
||||
if (type === "window")
|
||||
{
|
||||
var frame = CGRectMake([elementData valueForKey:@"originX"], [elementData valueForKey:@"originY"], [elementData valueForKey:@"width"], [elementData valueForKey:@"height"]);
|
||||
if (CGRectContainsPoint(frame, aPoint))
|
||||
return elementData;
|
||||
}
|
||||
}
|
||||
return nil;
|
||||
}
|
||||
|
||||
- (void)addNewElementOfType:(CPString)elementType atPoint:(CGPoint)aPoint
|
||||
{
|
||||
var newElementData = [CPConservativeDictionary dictionary];
|
||||
var containerData = [self _containerDataAtPoint:aPoint];
|
||||
var viewClass = [UIBuilderController classForElementType:elementType];
|
||||
|
||||
// Set default properties based on type
|
||||
[newElementData setValue:elementType forKey:@"type"];
|
||||
[newElementData setValue:@"id_" + _elementCounter++ forKey:@"id"];
|
||||
|
||||
// Set default values from the view class
|
||||
var defaultValues = [viewClass defaultValues];
|
||||
for (var key in defaultValues) {
|
||||
[newElementData setValue:defaultValues[key] forKey:key];
|
||||
}
|
||||
|
||||
// Set default sizes
|
||||
if (elementType === "window") {
|
||||
[newElementData setValue:250 forKey:@"width"];
|
||||
[newElementData setValue:200 forKey:@"height"];
|
||||
[newElementData setValue:[] forKey:@"children"];
|
||||
} else if (elementType === "button") {
|
||||
[newElementData setValue:100 forKey:@"width"];
|
||||
[newElementData setValue:24 forKey:@"height"];
|
||||
} else if (elementType === "slider") {
|
||||
[newElementData setValue:150 forKey:@"width"];
|
||||
[newElementData setValue:20 forKey:@"height"];
|
||||
} else { // textfield
|
||||
[newElementData setValue:150 forKey:@"width"];
|
||||
[newElementData setValue:22 forKey:@"height"];
|
||||
}
|
||||
|
||||
// Calculate centered position
|
||||
var elementWidth = [newElementData valueForKey:@"width"];
|
||||
var elementHeight = [newElementData valueForKey:@"height"];
|
||||
var centeredX = aPoint.x - (elementWidth / 2);
|
||||
var centeredY = aPoint.y - (elementHeight / 2);
|
||||
[newElementData setValue:centeredX forKey:@"originX"];
|
||||
[newElementData setValue:centeredY forKey:@"originY"];
|
||||
|
||||
if (containerData && elementType !== "window")
|
||||
{
|
||||
// Convert point to be relative to the container and center the element
|
||||
var elementWidth = [newElementData valueForKey:@"width"];
|
||||
var elementHeight = [newElementData valueForKey:@"height"];
|
||||
var relativeX = (aPoint.x - [containerData valueForKey:@"originX"]) - (elementWidth / 2);
|
||||
var relativeY = (aPoint.y - [containerData valueForKey:@"originY"]) - (elementHeight / 2);
|
||||
[newElementData setValue:relativeX forKey:@"originX"];
|
||||
[newElementData setValue:relativeY forKey:@"originY"];
|
||||
|
||||
// Add as a child to the container
|
||||
[newElementData setValue:[containerData valueForKey:@"id"] forKey:@"parentID"];
|
||||
[[containerData mutableArrayValueForKey:@"children"] addObject:newElementData];
|
||||
}
|
||||
|
||||
// Add to the main controller regardless, so selection works.
|
||||
[[[[CPApp keyWindow] undoManager] prepareWithInvocationTarget:_elementsController] removeObject:newElementData];
|
||||
[[[CPApp keyWindow] undoManager] setActionName:@"Add Element"];
|
||||
[_elementsController addObject:newElementData];
|
||||
|
||||
[_elementsController setSelectedObjects:[CPArray arrayWithObject:newElementData]];
|
||||
}
|
||||
|
||||
- (void)removeSelectedElementsWithActionName:(CPString)actionName
|
||||
{
|
||||
var selectedObjects = [[_elementsController selectedObjects] copy];
|
||||
if ([selectedObjects count] === 0) return;
|
||||
|
||||
[[[[CPApp keyWindow] undoManager] prepareWithInvocationTarget:_elementsController] addObjects:selectedObjects];
|
||||
[[[CPApp keyWindow] undoManager] setActionName:actionName];
|
||||
[_elementsController removeObjects:selectedObjects];
|
||||
}
|
||||
|
||||
- (void)removeSelectedElements
|
||||
{
|
||||
[self removeSelectedElementsWithActionName:@"Delete"];
|
||||
}
|
||||
|
||||
- (void)cut:(id)sender
|
||||
{
|
||||
[self copy:sender];
|
||||
[self removeSelectedElementsWithActionName:@"Cut"];
|
||||
}
|
||||
|
||||
#pragma mark -
|
||||
#pragma mark Keyboard Movement
|
||||
|
||||
- (void)moveSelectedElementsByDeltaX:(int)deltaX deltaY:(int)deltaY
|
||||
{
|
||||
var selectedDataObjects = [_elementsController selectedObjects];
|
||||
var changes = [CPMutableArray array];
|
||||
for (var i = 0; i < [selectedDataObjects count]; i++)
|
||||
{
|
||||
var data = selectedDataObjects[i];
|
||||
var newFrame = {
|
||||
origin: {
|
||||
x: [data valueForKey:@"originX"] + deltaX,
|
||||
y: [data valueForKey:@"originY"] + deltaY
|
||||
}
|
||||
};
|
||||
[changes addObject:{ data: data, frame: newFrame }];
|
||||
}
|
||||
[self applyFrameChanges:changes withActionName:@"Move"];
|
||||
}
|
||||
|
||||
- (void)moveLeft:(id)sender
|
||||
{
|
||||
[self moveSelectedElementsByDeltaX:-1 deltaY:0];
|
||||
}
|
||||
|
||||
- (void)moveRight:(id)sender
|
||||
{
|
||||
[self moveSelectedElementsByDeltaX:1 deltaY:0];
|
||||
}
|
||||
|
||||
- (void)moveUp:(id)sender
|
||||
{
|
||||
[self moveSelectedElementsByDeltaX:0 deltaY:-1];
|
||||
}
|
||||
|
||||
- (void)moveDown:(id)sender
|
||||
{
|
||||
[self moveSelectedElementsByDeltaX:0 deltaY:1];
|
||||
}
|
||||
|
||||
#pragma mark -
|
||||
#pragma mark Copy & Paste
|
||||
|
||||
- (void)copy:(id)sender
|
||||
{
|
||||
var selectedData = [_elementsController selectedObjects];
|
||||
|
||||
if ([selectedData count] > 0)
|
||||
{
|
||||
var pboard = [CPPasteboard generalPasteboard];
|
||||
var data = [CPKeyedArchiver archivedDataWithRootObject:selectedData];
|
||||
|
||||
// 1. Declare that you are providing BOTH a custom type and a standard string type.
|
||||
[pboard declareTypes:[UIBuilderElementPboardType, CPStringPboardType] owner:nil];
|
||||
|
||||
// 2. Set the data for your custom type, for your app's internal 'paste' to use.
|
||||
[pboard setData:data forType:UIBuilderElementPboardType];
|
||||
|
||||
// 3. Set a string representation for the browser and other applications.
|
||||
// This can be a simple description or a more complex JSON representation.
|
||||
var description = [selectedData count] + " UI element(s) copied.";
|
||||
[pboard setString:description forType:CPStringPboardType];
|
||||
}
|
||||
}
|
||||
|
||||
- (void)_assignNewIDsToElement:(CPMutableDictionary)elementData
|
||||
{
|
||||
[elementData setValue:@"id_" + _elementCounter++ forKey:@"id"];
|
||||
|
||||
var children = [elementData valueForKey:@"children"];
|
||||
if (children)
|
||||
{
|
||||
var newChildren = [CPMutableArray array];
|
||||
for (var i = 0; i < [children count]; i++)
|
||||
{
|
||||
var child = children[i];
|
||||
// Deep copy child before modifying
|
||||
var newChild = [CPKeyedUnarchiver unarchiveObjectWithData:[CPKeyedArchiver archivedDataWithRootObject:child]];
|
||||
[newChild setValue:[elementData valueForKey:@"id"] forKey:@"parentID"];
|
||||
[self _assignNewIDsToElement:newChild];
|
||||
[newChildren addObject:newChild];
|
||||
}
|
||||
[elementData setValue:newChildren forKey:@"children"];
|
||||
}
|
||||
}
|
||||
|
||||
- (void)paste:(id)sender
|
||||
{
|
||||
var pboard = [CPPasteboard generalPasteboard];
|
||||
var types = [pboard types];
|
||||
|
||||
if ([types containsObject:UIBuilderElementPboardType])
|
||||
{
|
||||
var data = [pboard dataForType:UIBuilderElementPboardType];
|
||||
var pastedElements = [CPKeyedUnarchiver unarchiveObjectWithData:data];
|
||||
var newSelection = [CPMutableArray array];
|
||||
|
||||
// Determine the target container
|
||||
var targetContainer = nil;
|
||||
var selectedObjects = [_elementsController selectedObjects];
|
||||
if ([selectedObjects count] > 0)
|
||||
{
|
||||
var firstSelected = selectedObjects[0];
|
||||
var parentID = [firstSelected valueForKey:@"parentID"];
|
||||
if (parentID)
|
||||
{
|
||||
// Find the parent container in the elements controller
|
||||
var allElements = [_elementsController arrangedObjects];
|
||||
for (var i = 0; i < [allElements count]; i++)
|
||||
{
|
||||
if ([[allElements[i] valueForKey:@"id"] isEqualToString:parentID])
|
||||
{
|
||||
targetContainer = allElements[i];
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
// If the selected object has no parent, it must be a window
|
||||
targetContainer = firstSelected;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
// If no selection, find the first window
|
||||
var allElements = [_elementsController arrangedObjects];
|
||||
for (var i = 0; i < [allElements count]; i++)
|
||||
{
|
||||
if ([[allElements[i] valueForKey:@"type"] isEqualToString:@"window"])
|
||||
{
|
||||
targetContainer = allElements[i];
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for (var i = 0; i < [pastedElements count]; i++)
|
||||
{
|
||||
var newElement = [CPKeyedUnarchiver unarchiveObjectWithData:[CPKeyedArchiver archivedDataWithRootObject:pastedElements[i]]];
|
||||
|
||||
[newElement setValue:[newElement valueForKey:@"originX"] + 10 forKey:@"originX"];
|
||||
[newElement setValue:[newElement valueForKey:@"originY"] + 10 forKey:@"originY"];
|
||||
|
||||
[self _assignNewIDsToElement:newElement];
|
||||
|
||||
if (targetContainer && [newElement valueForKey:@"type"] !== @"window")
|
||||
{
|
||||
[newElement setValue:[targetContainer valueForKey:@"id"] forKey:@"parentID"];
|
||||
[[targetContainer mutableArrayValueForKey:@"children"] addObject:newElement];
|
||||
}
|
||||
else
|
||||
{
|
||||
[newElement removeObjectForKey:@"parentID"];
|
||||
}
|
||||
|
||||
[_elementsController addObject:newElement];
|
||||
|
||||
if ([newElement valueForKey:@"children"])
|
||||
[_elementsController addObjects:[newElement valueForKey:@"children"]];
|
||||
|
||||
[newSelection addObject:newElement];
|
||||
}
|
||||
|
||||
[[[[CPApp keyWindow] undoManager] prepareWithInvocationTarget:_elementsController] removeObjects:newSelection];
|
||||
[[[CPApp keyWindow] undoManager] setActionName:@"Paste"];
|
||||
[_elementsController setSelectedObjects:newSelection];
|
||||
}
|
||||
}
|
||||
|
||||
- (void)addNewElementOfType:(CPString)elementType inNewWindowAtPoint:(CGPoint)aPoint
|
||||
{
|
||||
// 1. Create the new element to be placed in the window
|
||||
var newElementData = [CPConservativeDictionary dictionary];
|
||||
var viewClass = [UIBuilderController classForElementType:elementType];
|
||||
[newElementData setValue:elementType forKey:@"type"];
|
||||
[newElementData setValue:@"id_" + _elementCounter++ forKey:@"id"];
|
||||
|
||||
var defaultValues = [viewClass defaultValues];
|
||||
for (var key in defaultValues)
|
||||
[newElementData setValue:defaultValues[key] forKey:key];
|
||||
|
||||
var elementWidth, elementHeight;
|
||||
|
||||
if (elementType === "button") {
|
||||
elementWidth = 100;
|
||||
elementHeight = 24;
|
||||
} else if (elementType === "slider") {
|
||||
elementWidth = 150;
|
||||
elementHeight = 20;
|
||||
} else { // textfield
|
||||
elementWidth = 150;
|
||||
elementHeight = 22;
|
||||
}
|
||||
[newElementData setValue:elementWidth forKey:@"width"];
|
||||
[newElementData setValue:elementHeight forKey:@"height"];
|
||||
|
||||
// 2. Create the window that will contain the new element
|
||||
var windowData = [CPConservativeDictionary dictionary];
|
||||
var windowClass = [UIBuilderController classForElementType:"window"];
|
||||
var windowWidth = 250, windowHeight = 200;
|
||||
[windowData setValue:@"window" forKey:@"type"];
|
||||
[windowData setValue:@"id_" + _elementCounter++ forKey:@"id"];
|
||||
[windowData setValue:windowWidth forKey:@"width"];
|
||||
[windowData setValue:windowHeight forKey:@"height"];
|
||||
[windowData setValue:[] forKey:@"children"];
|
||||
|
||||
defaultValues = [windowClass defaultValues];
|
||||
for (var key in defaultValues) {
|
||||
[windowData setValue:defaultValues[key] forKey:key];
|
||||
}
|
||||
|
||||
// 3. Position the new element in the center of the window
|
||||
var elementX = (windowWidth - elementWidth) / 2;
|
||||
var elementY = (windowHeight - elementHeight) / 2;
|
||||
[newElementData setValue:elementX forKey:@"originX"];
|
||||
[newElementData setValue:elementY forKey:@"originY"];
|
||||
|
||||
// 4. Position the window so the element is at the drop point
|
||||
var windowX = aPoint.x - elementX;
|
||||
var windowY = aPoint.y - elementY;
|
||||
[windowData setValue:windowX forKey:@"originX"];
|
||||
[windowData setValue:windowY forKey:@"originY"];
|
||||
|
||||
// 5. Add the element to the window's children
|
||||
[newElementData setValue:[windowData valueForKey:@"id"] forKey:@"parentID"];
|
||||
[[windowData mutableArrayValueForKey:@"children"] addObject:newElementData];
|
||||
|
||||
console.log("UIBuilderController: addNewElementOfType:inNewWindowAtPoint: - Adding new element to window's children:", newElementData);
|
||||
|
||||
// 6. Add both to the elements controller
|
||||
var undoManager = [[CPApp keyWindow] undoManager];
|
||||
[undoManager beginUndoGrouping];
|
||||
[[undoManager prepareWithInvocationTarget:_elementsController] removeObject:newElementData];
|
||||
[[undoManager prepareWithInvocationTarget:_elementsController] removeObject:windowData];
|
||||
[undoManager setActionName:@"Add Element in New Window"];
|
||||
[_elementsController addObject:windowData];
|
||||
[_elementsController addObject:newElementData];
|
||||
[undoManager endUndoGrouping];
|
||||
|
||||
// 7. Select the new element
|
||||
[_elementsController setSelectedObjects:[CPArray arrayWithObject:newElementData]];
|
||||
}
|
||||
|
||||
- (void)addNewElementOfType:(CPString)elementType inWindow:(CPDictionary)windowData atPoint:(CGPoint)aPoint
|
||||
{
|
||||
console.log("UIBuilderController: addNewElementOfType:inWindow:atPoint: - Adding element ", elementType, " to window ", windowData, " at point ", aPoint);
|
||||
var newElementData = [CPConservativeDictionary dictionary];
|
||||
var viewClass = [UIBuilderController classForElementType:elementType];
|
||||
|
||||
[newElementData setValue:elementType forKey:@"type"];
|
||||
[newElementData setValue:@"id_" + _elementCounter++ forKey:@"id"];
|
||||
|
||||
var defaultValues = [viewClass defaultValues];
|
||||
for (var key in defaultValues)
|
||||
[newElementData setValue:defaultValues[key] forKey:key];
|
||||
|
||||
var elementWidth, elementHeight;
|
||||
|
||||
if (elementType === "button") {
|
||||
elementWidth = 100;
|
||||
elementHeight = 24;
|
||||
} else if (elementType === "slider") {
|
||||
elementWidth = 150;
|
||||
elementHeight = 20;
|
||||
} else { // textfield
|
||||
elementWidth = 150;
|
||||
elementHeight = 22;
|
||||
}
|
||||
[newElementData setValue:elementWidth forKey:@"width"];
|
||||
[newElementData setValue:elementHeight forKey:@"height"];
|
||||
|
||||
// Position the new element relative to the window's origin
|
||||
[newElementData setValue:aPoint.x forKey:@"originX"];
|
||||
[newElementData setValue:aPoint.y forKey:@"originY"];
|
||||
|
||||
// Add as a child to the container window
|
||||
[newElementData setValue:[windowData valueForKey:@"id"] forKey:@"parentID"];
|
||||
[[windowData mutableArrayValueForKey:@"children"] addObject:newElementData];
|
||||
|
||||
// Add to the main controller
|
||||
var undoManager = [[CPApp keyWindow] undoManager];
|
||||
[undoManager beginUndoGrouping];
|
||||
[[undoManager prepareWithInvocationTarget:_elementsController] removeObject:newElementData];
|
||||
[undoManager setActionName:@"Add Element to Window"];
|
||||
[_elementsController addObject:newElementData];
|
||||
[undoManager endUndoGrouping];
|
||||
|
||||
[_elementsController setSelectedObjects:[CPArray arrayWithObject:newElementData]];
|
||||
}
|
||||
|
||||
- (void)addConnectionFrom:(CPDictionary)sourceData to:(CPDictionary)targetData atPoint:(CGPoint)atPoint outlet:(CPString)outlet action:(CPString)action
|
||||
{
|
||||
var newConnection = [CPConservativeDictionary dictionary];
|
||||
[newConnection setValue:[sourceData valueForKey:@"id"] forKey:@"sourceID"];
|
||||
[newConnection setValue:[targetData valueForKey:@"id"] forKey:@"targetID"];
|
||||
[newConnection setValue:outlet forKey:@"outlet"];
|
||||
[newConnection setValue:action forKey:@"action"];
|
||||
[newConnection setValue:@"connection_" + _elementCounter++ forKey:@"id"];
|
||||
|
||||
if (atPoint)
|
||||
[newConnection setValue:{x: atPoint.x, y: atPoint.y} forKey:@"atPoint"];
|
||||
|
||||
[[[[CPApp keyWindow] undoManager] prepareWithInvocationTarget:_connectionsController] removeObject:newConnection];
|
||||
[[[CPApp keyWindow] undoManager] setActionName:@"Add Connection"];
|
||||
|
||||
[_connectionsController addObject:newConnection];
|
||||
|
||||
console.log("UIBuilderController: addConnectionFrom:to: - Added connection: ", newConnection);
|
||||
console.log("Connections controller count after add: " + [[_connectionsController arrangedObjects] count]);
|
||||
}
|
||||
|
||||
- (void)removeConnection:(CPDictionary)connection
|
||||
{
|
||||
[[[[CPApp keyWindow] undoManager] prepareWithInvocationTarget:_connectionsController] addObject:connection];
|
||||
[[[CPApp keyWindow] undoManager] setActionName:@"Remove Connection"];
|
||||
|
||||
[_connectionsController removeObject:connection];
|
||||
}
|
||||
|
||||
#pragma mark -
|
||||
#pragma mark UICanvasView Delegate Methods
|
||||
|
||||
- (void)applyFrameChanges:(CPArray)changes withActionName:(CPString)actionName
|
||||
{
|
||||
var undoManager = [[CPApp keyWindow] undoManager];
|
||||
var undoChanges = [CPMutableArray array];
|
||||
|
||||
[undoManager beginUndoGrouping];
|
||||
[undoManager setActionName:actionName];
|
||||
|
||||
for (var i = 0; i < [changes count]; i++)
|
||||
{
|
||||
var change = changes[i];
|
||||
var data = change.data;
|
||||
var newFrame = change.frame;
|
||||
var oldValues = { data: data, frame: {} };
|
||||
|
||||
if (newFrame.origin)
|
||||
{
|
||||
oldValues.frame.origin = {
|
||||
x: [data valueForKey:@"originX"],
|
||||
y: [data valueForKey:@"originY"]
|
||||
};
|
||||
[data setValue:newFrame.origin.x forKey:@"originX"];
|
||||
[data setValue:newFrame.origin.y forKey:@"originY"];
|
||||
}
|
||||
|
||||
if (newFrame.size)
|
||||
{
|
||||
oldValues.frame.size = {
|
||||
width: [data valueForKey:@"width"],
|
||||
height: [data valueForKey:@"height"]
|
||||
};
|
||||
[data setValue:newFrame.size.width forKey:@"width"];
|
||||
[data setValue:newFrame.size.height forKey:@"height"];
|
||||
}
|
||||
[undoChanges addObject:oldValues];
|
||||
}
|
||||
|
||||
[[undoManager prepareWithInvocationTarget:self] applyFrameChanges:undoChanges withActionName:actionName];
|
||||
[undoManager endUndoGrouping];
|
||||
}
|
||||
|
||||
- (void)canvasView:(UICanvasView)aCanvas didMoveElement:(UIElementView)anElement
|
||||
{
|
||||
var selectedViews = [aCanvas selectedSubViews];
|
||||
var changes = [CPMutableArray array];
|
||||
for (var i = 0; i < [selectedViews count]; i++)
|
||||
{
|
||||
var view = selectedViews[i];
|
||||
[changes addObject:{ data: [view dataObject], frame: { origin: [view frame].origin } }];
|
||||
}
|
||||
[self applyFrameChanges:changes withActionName:@"Move"];
|
||||
}
|
||||
|
||||
- (void)canvasView:(UICanvasView)aCanvas didResizeElement:(UIElementView)anElement
|
||||
{
|
||||
var changes = [CPMutableArray array];
|
||||
var frame = [anElement frame];
|
||||
[changes addObject:{ data: [anElement dataObject], frame: { origin: frame.origin, size: frame.size } }];
|
||||
[self applyFrameChanges:changes withActionName:@"Resize"];
|
||||
}
|
||||
|
||||
- (void)canvasView:(UICanvasView)aCanvas didConnectElement:(UIElementView)sourceElement toElement:(UIElementView)targetElement asTargetAction:(CPString)actionName
|
||||
{
|
||||
var sourceData = [sourceElement dataObject];
|
||||
var targetData = [targetElement dataObject];
|
||||
|
||||
// For a target-action, the outlet is typically 'target'
|
||||
var outletName = @"target";
|
||||
|
||||
[self addConnectionFrom:sourceData to:targetData atPoint:nil outlet:outletName action:actionName];
|
||||
}
|
||||
|
||||
- (void)canvasView:(UICanvasView)aCanvas didConnectElement:(UIElementView)sourceElement toElement:(UIElementView)targetElement asOutlet:(CPString)outletName
|
||||
{
|
||||
var sourceData = [sourceElement dataObject];
|
||||
var targetData = [targetElement dataObject];
|
||||
|
||||
// For a simple outlet connection, there is no action.
|
||||
var actionName = nil;
|
||||
|
||||
[self addConnectionFrom:sourceData to:targetData atPoint:nil outlet:outletName action:actionName];
|
||||
}
|
||||
|
||||
- (void)changeValue:(id)newValue forObject:(id)dataObject
|
||||
{
|
||||
var oldValue = [dataObject valueForKey:@"value"];
|
||||
if (oldValue != newValue)
|
||||
{
|
||||
var undoManager = [[CPApp keyWindow] undoManager];
|
||||
[[undoManager prepareWithInvocationTarget:self] changeValue:oldValue forObject:dataObject];
|
||||
[undoManager setActionName:@"Change Value"];
|
||||
[dataObject setValue:newValue forKey:@"value"];
|
||||
}
|
||||
}
|
||||
|
||||
- (void)changeValueForSelectedObject:(id)newValue
|
||||
{
|
||||
var selectedObjects = [[self elementsController] selectedObjects];
|
||||
if ([selectedObjects count] === 1)
|
||||
{
|
||||
[self changeValue:newValue forObject:selectedObjects[0]];
|
||||
}
|
||||
}
|
||||
|
||||
@end
|
||||
@@ -0,0 +1,996 @@
|
||||
//
|
||||
// UICanvasView.j
|
||||
// A full-window canvas for the UI Builder.
|
||||
//
|
||||
// By Daniel Boehringer in 2025.
|
||||
// - It acts as a drag-and-drop destination for new UI elements from the palette.
|
||||
// - It correctly instantiates different UIElementView subclasses based on the data model.
|
||||
//
|
||||
|
||||
@import "UIBuilderConstants.j";
|
||||
@import "UIElementView.j";
|
||||
@import "ConnectionView.j";
|
||||
@import "UIBuilderConstants.j";
|
||||
|
||||
function treshold(value, limit)
|
||||
{
|
||||
return value > 0 ? Math.min(value, limit) : Math.max(value, -limit);
|
||||
}
|
||||
|
||||
@implementation UICanvasView : CPView <CPMenuDelegate>
|
||||
{
|
||||
// Data binding ivars
|
||||
id _dataObjectsContainer;
|
||||
CPString _dataObjectsKeyPath;
|
||||
id _selectionIndexesContainer;
|
||||
CPString _selectionIndexesKeyPath;
|
||||
CPArray _oldDataObjects;
|
||||
|
||||
// Connections ivars
|
||||
id _connectionsContainer;
|
||||
CPString _connectionsKeyPath;
|
||||
CPArray _oldConnections;
|
||||
id _selectedConnectionsContainer;
|
||||
CPString _selectedConnectionsKeyPath;
|
||||
|
||||
// Rubber-band selection ivars
|
||||
CGPoint _rubberStart;
|
||||
CGPoint _rubberEnd;
|
||||
BOOL _isRubbing;
|
||||
|
||||
ConnectionView _connectionView;
|
||||
|
||||
id _delegate;
|
||||
|
||||
// Connection Menu ivars
|
||||
UIElementView _connectionSource;
|
||||
UIElementView _connectionTarget;
|
||||
BOOL _connectionMade;
|
||||
}
|
||||
|
||||
-(BOOL)acceptsFirstMouse:(CPEvent)aEvent
|
||||
{
|
||||
return YES;
|
||||
}
|
||||
|
||||
// KVO contexts
|
||||
var _propertyObservationContext = 1091;
|
||||
var _dataObjectsObservationContext = 1092;
|
||||
var _selectionIndexesObservationContext = 1093;
|
||||
var _connectionsObservationContext = 1094;
|
||||
var _selectedConnectionsObservationContext = 1095;
|
||||
|
||||
- (id)initWithFrame:(CGRect)aRect
|
||||
{
|
||||
self = [super initWithFrame:aRect];
|
||||
|
||||
if (self)
|
||||
{
|
||||
// Register to accept drops from the palette
|
||||
[self registerForDraggedTypes:[
|
||||
UIWindowDragType,
|
||||
UIButtonDragType,
|
||||
UISliderDragType,
|
||||
UITextFieldDragType
|
||||
]];
|
||||
|
||||
_connectionView = [[ConnectionView alloc] initWithFrame:[self bounds]];
|
||||
[_connectionView setAutoresizingMask:CPViewWidthSizable | CPViewHeightSizable];
|
||||
[self addSubview:_connectionView];
|
||||
}
|
||||
return self;
|
||||
}
|
||||
|
||||
#pragma mark - Bindings & KVO (Largely from EFLaceView)
|
||||
|
||||
+ (void)initialize
|
||||
{
|
||||
[self exposeBinding:"dataObjects"];
|
||||
[self exposeBinding:@"selectionIndexes"];
|
||||
[self exposeBinding:@"connections"];
|
||||
[self exposeBinding:@"selectedConnections"];
|
||||
}
|
||||
|
||||
- (void)bind:(CPString)bindingName toObject:(id)observableObject withKeyPath:(CPString)observableKeyPath options:(CPDictionary)options
|
||||
{
|
||||
if ([bindingName isEqualToString:@"dataObjects"])
|
||||
{
|
||||
_dataObjectsContainer = observableObject;
|
||||
_dataObjectsKeyPath = observableKeyPath;
|
||||
[_dataObjectsContainer addObserver:self forKeyPath:_dataObjectsKeyPath options:(CPKeyValueObservingOptionNew | CPKeyValueObservingOptionOld) context:_dataObjectsObservationContext];
|
||||
[self startObservingDataObjects:[self dataObjects]];
|
||||
_oldDataObjects = [[self dataObjects] copy] || @[];
|
||||
}
|
||||
else if ([bindingName isEqualToString:@"selectionIndexes"])
|
||||
{
|
||||
_selectionIndexesContainer = observableObject;
|
||||
_selectionIndexesKeyPath = observableKeyPath;
|
||||
[_selectionIndexesContainer addObserver:self forKeyPath:_selectionIndexesKeyPath options:CPKeyValueObservingOptionNew | CPKeyValueObservingOptionOld context:_selectionIndexesObservationContext];
|
||||
}
|
||||
else if ([bindingName isEqualToString:@"connections"])
|
||||
{
|
||||
_connectionsContainer = observableObject;
|
||||
_connectionsKeyPath = observableKeyPath;
|
||||
[_connectionsContainer addObserver:self forKeyPath:_connectionsKeyPath options:(CPKeyValueObservingOptionNew | CPKeyValueObservingOptionOld) context:_connectionsObservationContext];
|
||||
_oldConnections = [[self connections] copy] || @[];
|
||||
}
|
||||
else if ([bindingName isEqualToString:@"selectedConnections"])
|
||||
{
|
||||
_selectedConnectionsContainer = observableObject;
|
||||
_selectedConnectionsKeyPath = observableKeyPath;
|
||||
[_selectedConnectionsContainer addObserver:self forKeyPath:_selectedConnectionsKeyPath options:CPKeyValueObservingOptionNew | CPKeyValueObservingOptionOld context:_selectedConnectionsObservationContext];
|
||||
}
|
||||
else { [super bind:bindingName toObject:observableObject withKeyPath:observableKeyPath options:options]; }
|
||||
|
||||
[self setNeedsDisplay:YES];
|
||||
}
|
||||
|
||||
- (void)unbind:(CPString)bindingName
|
||||
{
|
||||
if ([bindingName isEqualToString:@"dataObjects"]) {
|
||||
[self stopObservingDataObjects:[self dataObjects]];
|
||||
[_dataObjectsContainer removeObserver:self forKeyPath:_dataObjectsKeyPath];
|
||||
_dataObjectsContainer = nil; _dataObjectsKeyPath = nil;
|
||||
} else if ([bindingName isEqualToString:@"selectionIndexes"]) {
|
||||
[_selectionIndexesContainer removeObserver:self forKeyPath:_selectionIndexesKeyPath];
|
||||
_selectionIndexesContainer = nil; _selectionIndexesKeyPath = nil;
|
||||
} else if ([bindingName isEqualToString:@"connections"]) {
|
||||
[_connectionsContainer removeObserver:self forKeyPath:_connectionsKeyPath];
|
||||
_connectionsContainer = nil; _connectionsKeyPath = nil;
|
||||
} else if ([bindingName isEqualToString:@"selectedConnections"]) {
|
||||
[_selectedConnectionsContainer removeObserver:self forKeyPath:_selectedConnectionsKeyPath];
|
||||
_selectedConnectionsContainer = nil; _selectedConnectionsKeyPath = nil;
|
||||
} else { [super unbind:bindingName]; }
|
||||
[self setNeedsDisplay:YES];
|
||||
}
|
||||
|
||||
- (CPArray)dataObjects
|
||||
{
|
||||
var result = [_dataObjectsContainer valueForKeyPath:_dataObjectsKeyPath];
|
||||
return (result == [CPNull null]) ? @[] : result;
|
||||
}
|
||||
|
||||
- (CPIndexSet)selectionIndexes
|
||||
{
|
||||
return [_selectionIndexesContainer valueForKeyPath:_selectionIndexesKeyPath];
|
||||
}
|
||||
|
||||
- (CPArray)connections
|
||||
{
|
||||
var result = [_connectionsContainer valueForKeyPath:_connectionsKeyPath];
|
||||
return (result == [CPNull null]) ? @[] : result;
|
||||
}
|
||||
|
||||
- (CPArray)selectedConnections
|
||||
{
|
||||
var result = [_selectedConnectionsContainer valueForKeyPath:_selectedConnectionsKeyPath];
|
||||
return (result == [CPNull null]) ? @[] : result;
|
||||
}
|
||||
|
||||
- (void)setSelectionIndexes:(CPIndexSet)indexes
|
||||
{
|
||||
[_selectionIndexesContainer setValue:indexes forKeyPath:_selectionIndexesKeyPath];
|
||||
}
|
||||
|
||||
- (void)startObservingDataObjects:(CPArray)dataObjects
|
||||
{
|
||||
if (!dataObjects || dataObjects == [CPNull null])
|
||||
return;
|
||||
|
||||
for (var i = 0; i < [dataObjects count]; i++)
|
||||
{
|
||||
var newDataObject = dataObjects[i];
|
||||
// Only create views for top-level objects. Children are handled by their parents.
|
||||
if (![newDataObject valueForKey:@"parentID"])
|
||||
[self _createViewForDataObject:newDataObject superview:self];
|
||||
}
|
||||
}
|
||||
|
||||
- (void)_createViewForDataObject:(CPDictionary)dataObject superview:(CPView)superview
|
||||
{
|
||||
var type = [dataObject valueForKey:@"type"];
|
||||
var newView;
|
||||
|
||||
// Instantiate the correct view based on the data model's 'type'
|
||||
if (type === "window")
|
||||
newView = [[UIWindowView alloc] init];
|
||||
else if (type === "button")
|
||||
newView = [[UIButtonView alloc] init];
|
||||
else if (type === "slider")
|
||||
newView = [[UISliderView alloc] init];
|
||||
else if (type === "textfield")
|
||||
newView = [[UITextFieldView alloc] init];
|
||||
else
|
||||
newView = [[UIElementView alloc] init]; // Fallback
|
||||
|
||||
[newView setDataObject:dataObject];
|
||||
|
||||
// Bind view properties to the data model
|
||||
[newView bind:@"originX" toObject:dataObject withKeyPath:@"originX" options:nil];
|
||||
[newView bind:@"originY" toObject:dataObject withKeyPath:@"originY" options:nil];
|
||||
[newView bind:@"width" toObject:dataObject withKeyPath:@"width" options:nil];
|
||||
[newView bind:@"height" toObject:dataObject withKeyPath:@"height" options:nil];
|
||||
|
||||
if (type === "window")
|
||||
{
|
||||
var children = [dataObject valueForKey:@"children"];
|
||||
for (var j = 0; j < [children count]; j++)
|
||||
{
|
||||
[self _createViewForDataObject:children[j] superview:newView];
|
||||
}
|
||||
}
|
||||
|
||||
[superview addSubview:newView];
|
||||
// i have no idea why this is needed, but it is to make the initial click work
|
||||
[CPApp._delegate._window makeKeyAndOrderFront:self];
|
||||
}
|
||||
|
||||
- (void)stopObservingDataObjects:(CPArray)dataObjects
|
||||
{
|
||||
if (!dataObjects || dataObjects == [CPNull null]) return;
|
||||
|
||||
var viewsToRemove = [CPMutableArray array];
|
||||
[self _findViewsForDataObjects:dataObjects inView:self foundViews:viewsToRemove];
|
||||
|
||||
for (var i = 0; i < [viewsToRemove count]; i++) {
|
||||
var viewToRemove = viewsToRemove[i];
|
||||
[self _removeViewAndChildren:viewToRemove];
|
||||
}
|
||||
}
|
||||
|
||||
- (void)_removeViewAndChildren:(UIElementView)viewToRemove
|
||||
{
|
||||
// Unbind everything before removing
|
||||
[viewToRemove unbind:@"value"];
|
||||
[viewToRemove unbind:@"originX"];
|
||||
[viewToRemove unbind:@"originY"];
|
||||
[viewToRemove unbind:@"width"];
|
||||
[viewToRemove unbind:@"height"];
|
||||
|
||||
var subviews = [[viewToRemove subviews] copy];
|
||||
for (var i = 0; i < [subviews count]; i++)
|
||||
{
|
||||
[self _removeViewAndChildren:subviews[i]];
|
||||
}
|
||||
|
||||
[viewToRemove removeFromSuperview];
|
||||
}
|
||||
|
||||
- (void)observeValueForKeyPath:(CPString)keyPath ofObject:(id)object change:(CPDictionary)change context:(id)context
|
||||
{
|
||||
if (context == _dataObjectsObservationContext)
|
||||
{
|
||||
var newDataObjects = [object valueForKeyPath:_dataObjectsKeyPath];
|
||||
var oldDataObjects = _oldDataObjects;
|
||||
|
||||
var added = [newDataObjects mutableCopy];
|
||||
[added removeObjectsInArray:oldDataObjects];
|
||||
[self startObservingDataObjects:added];
|
||||
|
||||
var removed = [oldDataObjects mutableCopy];
|
||||
[removed removeObjectsInArray:newDataObjects];
|
||||
[self stopObservingDataObjects:removed];
|
||||
|
||||
_oldDataObjects = [newDataObjects copy];
|
||||
[self setNeedsDisplay:YES];
|
||||
}
|
||||
else if (context == _selectionIndexesObservationContext)
|
||||
{
|
||||
var allDataObjects = [self dataObjects];
|
||||
var newIndexes = [change objectForKey:CPKeyValueChangeNewKey] || [CPIndexSet indexSet];
|
||||
var oldIndexes = [change objectForKey:CPKeyValueChangeOldKey] || [CPIndexSet indexSet];
|
||||
|
||||
// Find views for newly selected objects and redraw them
|
||||
var newSelectedDataObjects = [allDataObjects objectsAtIndexes:newIndexes];
|
||||
var newlySelectedViews = [CPMutableArray array];
|
||||
[self _findViewsForDataObjects:newSelectedDataObjects inView:self foundViews:newlySelectedViews];
|
||||
[newlySelectedViews makeObjectsPerformSelector:@selector(setNeedsDisplay:) withObject:YES];
|
||||
|
||||
// Find views for deselected objects and redraw them, but only if those objects still exist.
|
||||
var previouslySelectedViews = [CPMutableArray array];
|
||||
var oldSelectedDataObjects = [CPMutableArray array];
|
||||
var lastIndex = [oldIndexes lastIndex];
|
||||
|
||||
if (lastIndex != CPNotFound && lastIndex < [allDataObjects count])
|
||||
{
|
||||
oldSelectedDataObjects = [allDataObjects objectsAtIndexes:oldIndexes];
|
||||
}
|
||||
else
|
||||
{
|
||||
// If the indexes are out of bounds, it likely means the objects were deleted.
|
||||
// We need to find the views that were associated with the old indexes another way.
|
||||
// This is a tricky state to recover from. For now, we will just redraw all views.
|
||||
// A more sophisticated solution might involve caching view-data relationships.
|
||||
[[self subviews] makeObjectsPerformSelector:@selector(setNeedsDisplay:) withObject:YES];
|
||||
return;
|
||||
}
|
||||
|
||||
[self _findViewsForDataObjects:oldSelectedDataObjects inView:self foundViews:previouslySelectedViews];
|
||||
[previouslySelectedViews makeObjectsPerformSelector:@selector(setNeedsDisplay:) withObject:YES];
|
||||
}
|
||||
else if (context == _connectionsObservationContext)
|
||||
{
|
||||
var newConnections = [object valueForKeyPath:_connectionsKeyPath];
|
||||
var oldConnections = _oldConnections;
|
||||
|
||||
// For now, simply redraw all connections. A more optimized approach would be to only redraw changed connections.
|
||||
[self setNeedsDisplay:YES];
|
||||
_oldConnections = [newConnections copy];
|
||||
}
|
||||
else if (context == _selectedConnectionsObservationContext)
|
||||
{
|
||||
[self setNeedsDisplay:YES];
|
||||
}
|
||||
}
|
||||
|
||||
#pragma mark - Drawing & Mouse
|
||||
|
||||
- (void)drawRect:(CPRect)rect
|
||||
{
|
||||
// === START: Infographic Drawing ===
|
||||
|
||||
var bounds = [self bounds];
|
||||
|
||||
// 1. Define text attributes for the infographic
|
||||
var titleFont = [CPFont fontWithName:@"Helvetica-Bold" size:36];
|
||||
var subtitleFont = [CPFont fontWithName:@"Helvetica" size:18];
|
||||
var featureFont = [CPFont fontWithName:@"Helvetica" size:14];
|
||||
var watermarkColor = [CPColor colorWithCalibratedWhite:0.85 alpha:1.0]; // A light gray for the watermark effect
|
||||
|
||||
var titleAttributes = @{
|
||||
CPFontAttributeName: titleFont,
|
||||
CPForegroundColorAttributeName: watermarkColor
|
||||
};
|
||||
var subtitleAttributes = @{
|
||||
CPFontAttributeName: subtitleFont,
|
||||
CPForegroundColorAttributeName: watermarkColor
|
||||
};
|
||||
var featureAttributes = @{
|
||||
CPFontAttributeName: featureFont,
|
||||
CPForegroundColorAttributeName: watermarkColor
|
||||
};
|
||||
|
||||
// 2. Prepare the text content
|
||||
var title = @"Cappuccino JS";
|
||||
var subtitle = @"Desktop-Quality Applications in the Browser";
|
||||
var features = [
|
||||
@"• Drag-and-Drop UI Creation",
|
||||
@"• Direct Manipulation: Move & Resize (Keyboard / Mouse)",
|
||||
@"• Undo/Redo & Keyboard Navigation",
|
||||
@"• Control-Draggin -> Target-Action & Outlet Connections",
|
||||
@"• Context sensitive inspector panel",
|
||||
@"• Run the 'real thing' in a separate native window",
|
||||
@"• Source: https://github.com/daboe01/UIBuilder"
|
||||
|
||||
];
|
||||
|
||||
// 3. Calculate positions and draw the text, centering it on the canvas
|
||||
var titleSize = [title sizeWithAttributes:titleAttributes];
|
||||
var subtitleSize = [subtitle sizeWithAttributes:subtitleAttributes];
|
||||
var totalHeight = titleSize.height + subtitleSize.height + ([features count] * 20) + 40; // Approximate total height
|
||||
var currentY = (bounds.size.height - totalHeight) / 2.0;
|
||||
|
||||
// Draw Title
|
||||
var titlePoint = CGPointMake((bounds.size.width - titleSize.width) / 2.0, currentY);
|
||||
[title drawAtPoint:titlePoint withAttributes:titleAttributes];
|
||||
currentY += titleSize.height + 10;
|
||||
|
||||
// Draw Subtitle
|
||||
var subtitlePoint = CGPointMake((bounds.size.width - subtitleSize.width) / 2.0, currentY);
|
||||
[subtitle drawAtPoint:subtitlePoint withAttributes:subtitleAttributes];
|
||||
currentY += subtitleSize.height + 30;
|
||||
|
||||
// Draw Feature List
|
||||
for (var i = 0; i < [features count]; i++) {
|
||||
var feature = features[i];
|
||||
var featureSize = [feature sizeWithAttributes:featureAttributes];
|
||||
var featurePoint = CGPointMake((bounds.size.width - featureSize.width) / 2.0, currentY);
|
||||
[feature drawAtPoint:featurePoint withAttributes:featureAttributes];
|
||||
currentY += featureSize.height + 5;
|
||||
}
|
||||
|
||||
// === END: Infographic Drawing ===
|
||||
|
||||
// The background is drawn by the window. We only draw the rubber-band.
|
||||
if (_isRubbing)
|
||||
{
|
||||
var rubber = CGRectUnion(CGRectMake(_rubberStart.x, _rubberStart.y, 0.1, 0.1), CGRectMake(_rubberEnd.x, _rubberEnd.y, 0.1, 0.1));
|
||||
[[[[CPColor alternateSelectedControlColor] colorWithAlphaComponent:0.2] setFill]];
|
||||
[CPBezierPath fillRect:rubber];
|
||||
[[CPColor alternateSelectedControlColor] setStroke];
|
||||
[CPBezierPath setDefaultLineWidth:1.0];
|
||||
[CPBezierPath strokeRect:rubber];
|
||||
}
|
||||
|
||||
// Draw existing connections that are selected in the connections controller.
|
||||
var selectedConnections = [self selectedConnections];
|
||||
|
||||
if (selectedConnections && [selectedConnections count] > 0)
|
||||
{
|
||||
for (var i = 0; i < [selectedConnections count]; i++)
|
||||
{
|
||||
var connection = [selectedConnections objectAtIndex:i];
|
||||
var sourceID = [connection valueForKey:@"sourceID"];
|
||||
var targetID = [connection valueForKey:@"targetID"];
|
||||
var sourceView = [self viewForElementWithID:sourceID];
|
||||
var targetView = [self viewForElementWithID:targetID];
|
||||
|
||||
if (sourceView && targetView)
|
||||
{
|
||||
var startPoint = [sourceView convertPoint:CGPointMake(CGRectGetMidX([sourceView bounds]), CGRectGetMidY([sourceView bounds])) toView:self];
|
||||
var endPoint;
|
||||
var connectionPoint = [connection valueForKey:@"atPoint"];
|
||||
|
||||
if (connectionPoint) {
|
||||
endPoint = CGPointMake(connectionPoint.x, connectionPoint.y);
|
||||
} else {
|
||||
endPoint = [targetView convertPoint:CGPointMake(CGRectGetMidX([targetView bounds]), CGRectGetMidY([targetView bounds])) toView:self];
|
||||
}
|
||||
|
||||
// Draw the link with a distinct color, like blue.
|
||||
[self drawLinkFrom:startPoint to:endPoint color:[CPColor blueColor]];
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
- (void)drawLinkFrom:(CGPoint)startPoint to:(CGPoint)endPoint color:(CPColor)insideColor
|
||||
{
|
||||
|
||||
var dist = Math.sqrt(Math.pow(startPoint.x - endPoint.x, 2) + Math.pow(startPoint.y - endPoint.y, 2));
|
||||
|
||||
// a lace is made of an outside gray line of width 5, and a inside insideColor(ed) line of width 3
|
||||
var p0 = CGPointMake(startPoint.x, startPoint.y);
|
||||
var p3 = CGPointMake(endPoint.x, endPoint.y);
|
||||
|
||||
var p1 = CGPointMake(startPoint.x + treshold((endPoint.x - startPoint.x) / 2, 50), startPoint.y);
|
||||
var p2 = CGPointMake(endPoint.x - treshold((endPoint.x - startPoint.x) / 2, 50), endPoint.y);
|
||||
|
||||
// p0 and p1 are on the same horizontal line
|
||||
// distance between p0 and p1 is set with the treshold fuction
|
||||
// the same holds for p2 and p3
|
||||
|
||||
var path = [CPBezierPath bezierPath];
|
||||
[path setLineWidth:0];
|
||||
[[CPColor grayColor] set];
|
||||
[path appendBezierPathWithOvalInRect:CGRectMake(startPoint.x-2.5,startPoint.y-2.5,5,5)];
|
||||
[path fill];
|
||||
|
||||
path = [CPBezierPath bezierPath];
|
||||
[path setLineWidth:0];
|
||||
[insideColor set];
|
||||
[path appendBezierPathWithOvalInRect:CGRectMake(startPoint.x-1.5,startPoint.y-1.5,3,3)];
|
||||
[path fill];
|
||||
|
||||
path = [CPBezierPath bezierPath];
|
||||
[path setLineWidth:0];
|
||||
[[CPColor grayColor] set];
|
||||
[path appendBezierPathWithOvalInRect:CGRectMake(endPoint.x-2.5,endPoint.y-2.5,5,5)];
|
||||
[path fill];
|
||||
|
||||
path = [CPBezierPath bezierPath];
|
||||
[path setLineWidth:0];
|
||||
[insideColor set];
|
||||
[path appendBezierPathWithOvalInRect:CGRectMake(endPoint.x-1.5,endPoint.y-1.5,3,3)];
|
||||
[path fill];
|
||||
|
||||
// if the line is rather short, draw a straight line. the curve would look rather odd in this case.
|
||||
if (dist < 40)
|
||||
{
|
||||
path = [CPBezierPath bezierPath];
|
||||
[path setLineWidth:5];
|
||||
[path moveToPoint:startPoint];
|
||||
[path lineToPoint:endPoint];
|
||||
[[CPColor grayColor] set];
|
||||
[path stroke];
|
||||
|
||||
path = [CPBezierPath bezierPath];
|
||||
[path setLineWidth:3];
|
||||
[path moveToPoint:startPoint];
|
||||
[path lineToPoint:endPoint];
|
||||
[insideColor set];
|
||||
[path stroke];
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
path = [CPBezierPath bezierPath];
|
||||
[path setLineWidth:5];
|
||||
[path moveToPoint:p0];
|
||||
[path curveToPoint:p3 controlPoint1:p1 controlPoint2:p2];
|
||||
[[CPColor grayColor] set];
|
||||
[path stroke];
|
||||
|
||||
path = [CPBezierPath bezierPath];
|
||||
[path setLineWidth:3];
|
||||
[path moveToPoint:p0];
|
||||
[path curveToPoint:p3 controlPoint1:p1 controlPoint2:p2];
|
||||
[insideColor set];
|
||||
[path stroke];
|
||||
}
|
||||
|
||||
#pragma mark - View Lookup
|
||||
|
||||
// Private recursive helper method to search the entire view hierarchy.
|
||||
- (UIElementView)_findViewForElementWithID:(CPString)elementID inView:(CPView)aView
|
||||
{
|
||||
// Iterate through all subviews of the current view
|
||||
var subviews = [aView subviews];
|
||||
for (var i = 0; i < [subviews count]; i++)
|
||||
{
|
||||
var subview = subviews[i];
|
||||
|
||||
// We are only interested in UIElementView subclasses
|
||||
if (![subview isKindOfClass:[UIElementView class]])
|
||||
continue;
|
||||
|
||||
// 1. Check if the current subview is the one we are looking for.
|
||||
if ([[subview dataObject] valueForKey:@"id"] === elementID)
|
||||
{
|
||||
return subview; // Found it!
|
||||
}
|
||||
|
||||
// 2. If not, and this subview has children, recurse into it.
|
||||
// This is the key step to search inside containers like UIWindowView.
|
||||
if ([[subview subviews] count] > 0)
|
||||
{
|
||||
var foundView = [self _findViewForElementWithID:elementID inView:subview];
|
||||
if (foundView)
|
||||
{
|
||||
return foundView; // Found it in a nested hierarchy.
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// If we've searched this entire branch and found nothing, return nil.
|
||||
return nil;
|
||||
}
|
||||
|
||||
// Public method to start the search from the canvas itself.
|
||||
- (UIElementView)viewForElementWithID:(CPString)elementID
|
||||
{
|
||||
if (!elementID)
|
||||
return nil;
|
||||
|
||||
// Start the recursive search from the top-level canvas view.
|
||||
return [self _findViewForElementWithID:elementID inView:self];
|
||||
}
|
||||
|
||||
- (void)drawConnectionFrom:(CGPoint)startPoint to:(CGPoint)endPoint
|
||||
{
|
||||
[_connectionView setStartPoint:startPoint];
|
||||
[_connectionView setEndPoint:endPoint];
|
||||
[_connectionView setHidden:NO]; // Ensure it's visible when drawing
|
||||
[self addSubview:_connectionView]; // Bring to front
|
||||
[_connectionView setNeedsDisplay:YES];
|
||||
}
|
||||
|
||||
- (void)clearConnection
|
||||
{
|
||||
[_connectionView setHidden:YES];
|
||||
[_connectionView setNeedsDisplay:YES]; // Request redraw to clear old line
|
||||
}
|
||||
|
||||
- (UIElementView)viewAtPoint:(CGPoint)aPoint
|
||||
{
|
||||
return [self _findDeepestUIElementViewAtPoint:aPoint inView:self];
|
||||
}
|
||||
|
||||
- (UIElementView)_findDeepestUIElementViewAtPoint:(CGPoint)aPoint inView:(CPView)currentView
|
||||
{
|
||||
// Iterate through subviews in reverse order to get the topmost view
|
||||
for (var i = [[currentView subviews] count] - 1; i >= 0; i--)
|
||||
{
|
||||
var subview = [[currentView subviews] objectAtIndex:i];
|
||||
|
||||
// Convert the point to the subview's coordinate system
|
||||
var localPoint = [subview convertPoint:aPoint fromView:currentView];
|
||||
|
||||
if ([subview isKindOfClass:[UIElementView class]])
|
||||
{
|
||||
if (CGRectContainsPoint([subview bounds], localPoint))
|
||||
{
|
||||
// If this is a container view, recursively search its subviews
|
||||
if (subview._isContainer)
|
||||
{
|
||||
var deepestView = [self _findDeepestUIElementViewAtPoint:localPoint inView:subview];
|
||||
|
||||
if (deepestView)
|
||||
return deepestView;
|
||||
}
|
||||
// If not a container, or no deeper view found, return this view
|
||||
return subview;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return nil;
|
||||
}
|
||||
|
||||
- (void)mouseDown:(CPEvent)theEvent
|
||||
{
|
||||
if (_connectionSource)
|
||||
{
|
||||
[self menuDidEndTracking:nil];
|
||||
return;
|
||||
}
|
||||
// A click on the canvas background starts a rubber-band selection.
|
||||
[self deselectViews];
|
||||
_isRubbing = YES;
|
||||
_rubberStart = [self convertPoint:[theEvent locationInWindow] fromView:nil];
|
||||
_rubberEnd = _rubberStart;
|
||||
|
||||
[CPApp setTarget:self selector:@selector(_dragOpenSpaceWithEvent:) forNextEventMatchingMask:CPLeftMouseDraggedMask | CPLeftMouseUpMask untilDate:nil inMode:nil dequeue:YES];
|
||||
}
|
||||
|
||||
- (void)_dragOpenSpaceWithEvent:(CPEvent)theEvent
|
||||
{
|
||||
var mouseLoc = [self convertPoint:[theEvent locationInWindow] fromView:nil];
|
||||
_rubberEnd = mouseLoc;
|
||||
var rubberRect = CGRectUnion(CGRectMake(_rubberStart.x, _rubberStart.y, 1, 1), CGRectMake(_rubberEnd.x, _rubberEnd.y, 1, 1));
|
||||
|
||||
switch ([theEvent type])
|
||||
{
|
||||
case CPLeftMouseDragged:
|
||||
var indexesToSelect = [CPMutableIndexSet indexSet];
|
||||
var allDataObjects = [self dataObjects];
|
||||
for (var i = 0; i < [[self subviews] count]; i++) {
|
||||
var aView = [self subviews][i];
|
||||
if ([aView isKindOfClass:[UIElementView class]] && CGRectIntersectsRect([aView frame], rubberRect)) {
|
||||
var dataIndex = [allDataObjects indexOfObject:[aView dataObject]];
|
||||
if (dataIndex != CPNotFound) {
|
||||
[indexesToSelect addIndex:dataIndex];
|
||||
}
|
||||
}
|
||||
}
|
||||
[_selectionIndexesContainer setValue:indexesToSelect forKeyPath:_selectionIndexesKeyPath];
|
||||
[self setNeedsDisplay:YES];
|
||||
[CPApp setTarget:self selector:@selector(_dragOpenSpaceWithEvent:) forNextEventMatchingMask:CPLeftMouseDraggedMask | CPLeftMouseUpMask untilDate:nil inMode:nil dequeue:YES];
|
||||
break;
|
||||
|
||||
case CPLeftMouseUp:
|
||||
_isRubbing = NO;
|
||||
[self setNeedsDisplay:YES];
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
- (void)delete:(id)sender
|
||||
{
|
||||
// Forward the delete action to the delegate/controller
|
||||
if (_delegate && [_delegate respondsToSelector:@selector(removeSelectedElements)]) {
|
||||
[_delegate removeSelectedElements];
|
||||
}
|
||||
}
|
||||
|
||||
- (void)cut:(id)sender
|
||||
{
|
||||
if (_delegate && [_delegate respondsToSelector:@selector(cut:)]) {
|
||||
[_delegate cut:sender];
|
||||
}
|
||||
}
|
||||
|
||||
- (void)copy:(id)sender
|
||||
{
|
||||
if (_delegate && [_delegate respondsToSelector:@selector(copy:)]) {
|
||||
[_delegate copy:sender];
|
||||
}
|
||||
}
|
||||
|
||||
- (void)paste:(id)sender
|
||||
{
|
||||
if (_delegate && [_delegate respondsToSelector:@selector(paste:)]) {
|
||||
[_delegate paste:sender];
|
||||
}
|
||||
}
|
||||
|
||||
- (void)viewDidMoveToWindow
|
||||
{
|
||||
[super viewDidMoveToWindow];
|
||||
|
||||
if ([self window])
|
||||
{
|
||||
[[self window] makeFirstResponder:self];
|
||||
}
|
||||
}
|
||||
|
||||
- (BOOL)acceptsFirstResponder
|
||||
{
|
||||
return YES;
|
||||
}
|
||||
|
||||
/*
|
||||
- (BOOL)validateMenuItem:(CPMenuItem)aMenuItem
|
||||
{
|
||||
var action = [aMenuItem action];
|
||||
|
||||
if (action == @selector(copy:) || action == @selector(cut:) || action == @selector(delete:))
|
||||
{
|
||||
return [[self selectionIndexes] count] > 0;
|
||||
}
|
||||
|
||||
if (action == @selector(paste:))
|
||||
{
|
||||
return [[[CPPasteboard generalPasteboard] types] containsObject:UIBuilderElementPboardType];
|
||||
}
|
||||
|
||||
if (action == @selector(createTargetActionConnection:) || action == @selector(createOutletConnection:))
|
||||
{
|
||||
return YES;
|
||||
}
|
||||
|
||||
var undoManager = [[self window] undoManager];
|
||||
|
||||
if (action == @selector(undo:))
|
||||
{
|
||||
return [undoManager canUndo];
|
||||
}
|
||||
|
||||
if (action == @selector(redo:))
|
||||
{
|
||||
return [undoManager canRedo];
|
||||
}
|
||||
|
||||
return [super validateMenuItem:aMenuItem];
|
||||
}
|
||||
*/
|
||||
|
||||
- (void)keyDown:(CPEvent)theEvent
|
||||
{
|
||||
var characters = [theEvent characters];
|
||||
var flags = [theEvent modifierFlags];
|
||||
var selectors = [CPKeyBinding selectorsForKey:characters modifierFlags:flags];
|
||||
var delegate = [self delegate];
|
||||
var handled = NO;
|
||||
|
||||
if (selectors && delegate)
|
||||
{
|
||||
for (var i = 0; i < [selectors count]; i++)
|
||||
{
|
||||
var selectorName = selectors[i];
|
||||
if ([delegate respondsToSelector:selectorName])
|
||||
{
|
||||
[delegate performSelector:selectorName withObject:self];
|
||||
handled = YES;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (!handled)
|
||||
[super keyDown:theEvent];
|
||||
}
|
||||
|
||||
#pragma mark - Drag and Drop Destination
|
||||
|
||||
- (CPDragOperation)draggingEntered:(CPDraggingInfo)sender
|
||||
{
|
||||
// We accept any of the registered types
|
||||
return CPDragOperationCopy;
|
||||
}
|
||||
|
||||
- (BOOL)performDragOperation:(CPDraggingInfo)sender
|
||||
{
|
||||
var dropPoint = [self convertPoint:[sender draggingLocation] fromView:nil];
|
||||
var pasteboard = [sender draggingPasteboard];
|
||||
var types = [pasteboard types];
|
||||
var draggedType = types[0]; // Assuming only one type is being dragged
|
||||
var elementType;
|
||||
|
||||
if (draggedType === UIWindowDragType) elementType = "window";
|
||||
else if (draggedType === UIButtonDragType) elementType = "button";
|
||||
else if (draggedType === UISliderDragType) elementType = "slider";
|
||||
else if (draggedType === UITextFieldDragType) elementType = "textfield";
|
||||
|
||||
if (elementType && _delegate)
|
||||
{
|
||||
if (elementType === "window") {
|
||||
if ([_delegate respondsToSelector:@selector(addNewElementOfType:atPoint:)])
|
||||
{
|
||||
[_delegate addNewElementOfType:elementType atPoint:dropPoint];
|
||||
[self setNeedsDisplay:YES];
|
||||
return YES;
|
||||
}
|
||||
} else {
|
||||
if ([_delegate respondsToSelector:@selector(addNewElementOfType:inNewWindowAtPoint:)])
|
||||
{
|
||||
[_delegate addNewElementOfType:elementType inNewWindowAtPoint:dropPoint];
|
||||
[self setNeedsDisplay:YES];
|
||||
return YES;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return NO;
|
||||
}
|
||||
|
||||
#pragma mark - Delegate & Selection Management
|
||||
|
||||
- (id)delegate { return _delegate; }
|
||||
- (void)setDelegate:(id)newDelegate { _delegate = newDelegate; }
|
||||
|
||||
- (void)deselectViews
|
||||
{
|
||||
[_selectionIndexesContainer setValue:nil forKeyPath:_selectionIndexesKeyPath];
|
||||
}
|
||||
|
||||
- (void)selectView:(UIElementView)aView state:(BOOL)select
|
||||
{
|
||||
var selection = [[self selectionIndexes] mutableCopy] || [CPMutableIndexSet indexSet];
|
||||
var dataObjectIndex = [[self dataObjects] indexOfObject:[aView dataObject]];
|
||||
|
||||
|
||||
|
||||
if (dataObjectIndex != CPNotFound)
|
||||
{
|
||||
if (select)
|
||||
[selection addIndex:dataObjectIndex];
|
||||
|
||||
else [selection removeIndex:dataObjectIndex];
|
||||
}
|
||||
|
||||
[_selectionIndexesContainer setValue:selection forKeyPath:_selectionIndexesKeyPath];
|
||||
}
|
||||
|
||||
- (CPArray)selectedSubViews
|
||||
{
|
||||
var selectedDataObjects = [[self dataObjects] objectsAtIndexes:[self selectionIndexes]];
|
||||
var selectedViews = [CPMutableArray array];
|
||||
|
||||
[self _findViewsForDataObjects:selectedDataObjects inView:self foundViews:selectedViews];
|
||||
|
||||
return selectedViews;
|
||||
}
|
||||
|
||||
- (BOOL)isViewSelected:(CPView)aView
|
||||
{
|
||||
var selected = [self selectedSubViews];
|
||||
|
||||
return [selected containsObject:aView];
|
||||
}
|
||||
|
||||
- (void)_findViewsForDataObjects:(CPArray)dataObjects inView:(CPView)aView foundViews:(CPMutableArray)foundViews
|
||||
{
|
||||
var subviews = [aView subviews];
|
||||
|
||||
for (var i = 0; i < [subviews count]; i++)
|
||||
{
|
||||
var subview = subviews[i];
|
||||
|
||||
// Skip the connection view and any other non-UIElementView instances
|
||||
if (![subview isKindOfClass:[UIElementView class]])
|
||||
continue;
|
||||
|
||||
var contains = [dataObjects containsObject:[subview dataObject]];
|
||||
|
||||
if (contains)
|
||||
{
|
||||
[foundViews addObject:subview];
|
||||
}
|
||||
|
||||
// Recurse into subviews
|
||||
[self _findViewsForDataObjects:dataObjects inView:subview foundViews:foundViews];
|
||||
}
|
||||
}
|
||||
|
||||
// These methods are called by the UIElementView children to notify the controller
|
||||
- (void)elementDidMove:(UIElementView)anElement
|
||||
{
|
||||
if (_delegate && [_delegate respondsToSelector:@selector(canvasView:didMoveElement:)]) {
|
||||
[_delegate canvasView:self didMoveElement:anElement];
|
||||
}
|
||||
}
|
||||
|
||||
- (void)elementDidResize:(UIElementView)anElement
|
||||
{
|
||||
if (_delegate && [_delegate respondsToSelector:@selector(canvasView:didResizeElement:)]) {
|
||||
[_delegate canvasView:self didResizeElement:anElement];
|
||||
}
|
||||
}
|
||||
|
||||
- (void)elementDidConnect:(UIElementView)sourceElement to:(UIElementView)targetElement atPoint:(CGPoint)aPoint
|
||||
{
|
||||
if (_delegate && [_delegate respondsToSelector:@selector(canvasView:didConnectElement:toElement:atPoint:)]) {
|
||||
[_delegate canvasView:self didConnectElement:sourceElement toElement:targetElement atPoint:aPoint];
|
||||
}
|
||||
}
|
||||
|
||||
#pragma mark - Connection Menu
|
||||
|
||||
- (void)showConnectionMenuForSource:(UIElementView)sourceView target:(UIElementView)targetView at:(CGPoint)aPoint
|
||||
{
|
||||
_connectionSource = sourceView;
|
||||
_connectionTarget = targetView;
|
||||
_connectionMade = NO;
|
||||
|
||||
var menu = [[CPMenu alloc] initWithTitle:@"Connection Menu"];
|
||||
[menu setDelegate:self];
|
||||
|
||||
// 1. Add Target's Actions
|
||||
var targetActions = [[_connectionTarget dataObject] valueForKey:@"actions"];
|
||||
if (targetActions && [targetActions length] > 0)
|
||||
{
|
||||
var actionsArray = [targetActions componentsSeparatedByString:@", "];
|
||||
for (var i = 0; i < [actionsArray count]; i++)
|
||||
{
|
||||
var actionName = actionsArray[i];
|
||||
var menuItem = [[CPMenuItem alloc] initWithTitle:actionName action:@selector(createTargetActionConnection:) keyEquivalent:@""];
|
||||
[menu addItem:menuItem];
|
||||
}
|
||||
}
|
||||
|
||||
// 2. Add Separator
|
||||
if ([menu numberOfItems] > 0)
|
||||
[menu addItem:[CPMenuItem separatorItem]];
|
||||
|
||||
// 3. Add Source's Outlets
|
||||
var sourceOutlets = [[_connectionSource dataObject] valueForKey:@"outlets"];
|
||||
if (sourceOutlets && [sourceOutlets length] > 0)
|
||||
{
|
||||
var outletsArray = [sourceOutlets componentsSeparatedByString:@", "];
|
||||
for (var i = 0; i < [outletsArray count]; i++)
|
||||
{
|
||||
var outletName = outletsArray[i];
|
||||
if (outletName === @"target") continue; // Skip 'target' outlet as requested
|
||||
var menuItem = [[CPMenuItem alloc] initWithTitle:outletName action:@selector(createOutletConnection:) keyEquivalent:@""];
|
||||
[menu addItem:menuItem];
|
||||
}
|
||||
}
|
||||
|
||||
if ([menu numberOfItems] > 0)
|
||||
{
|
||||
[CPMenu popUpContextMenu:menu withEvent:[CPApp currentEvent] forView:self];
|
||||
}
|
||||
else
|
||||
{
|
||||
[self menuDidEndTracking:menu]; // No items, so clean up immediately
|
||||
}
|
||||
}
|
||||
|
||||
- (void)createTargetActionConnection:(CPMenuItem)sender
|
||||
{
|
||||
var actionName = [sender title];
|
||||
if (_delegate && [_delegate respondsToSelector:@selector(canvasView:didConnectElement:toElement:asTargetAction:)])
|
||||
{
|
||||
_connectionMade = YES;
|
||||
[self clearConnection];
|
||||
if (_connectionTarget)
|
||||
[_connectionTarget setAsDropTarget:NO];
|
||||
|
||||
[_delegate canvasView:self didConnectElement:_connectionSource toElement:_connectionTarget asTargetAction:actionName];
|
||||
}
|
||||
}
|
||||
|
||||
- (void)createOutletConnection:(CPMenuItem)sender
|
||||
{
|
||||
var outletName = [sender title];
|
||||
if (_delegate && [_delegate respondsToSelector:@selector(canvasView:didConnectElement:toElement:asOutlet:)])
|
||||
{
|
||||
_connectionMade = YES;
|
||||
[self clearConnection];
|
||||
if (_connectionTarget)
|
||||
[_connectionTarget setAsDropTarget:NO];
|
||||
|
||||
[_delegate canvasView:self didConnectElement:_connectionSource toElement:_connectionTarget asOutlet:outletName];
|
||||
}
|
||||
}
|
||||
|
||||
- (void)menuDidEndTracking:(CPMenu)aMenu
|
||||
{
|
||||
// This delegate method is called after a menu item is selected OR the menu is cancelled.
|
||||
if (!_connectionMade)
|
||||
{
|
||||
[self clearConnection];
|
||||
if (_connectionTarget)
|
||||
[_connectionTarget setAsDropTarget:NO];
|
||||
}
|
||||
|
||||
// Reset state
|
||||
_connectionSource = nil;
|
||||
_connectionTarget = nil;
|
||||
_connectionMade = NO;
|
||||
|
||||
[self setNeedsDisplay:YES];
|
||||
}
|
||||
|
||||
@end
|
||||
File diff suppressed because it is too large.
Load diff
@@ -0,0 +1,164 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="utf-8">
|
||||
|
||||
<!--[if lte IE 8]>
|
||||
<meta http-equiv="X-UA-Compatible" content="IE=EmulateIE7, chrome=1">
|
||||
<![endif]-->
|
||||
<!--[if gte IE 9]>
|
||||
<meta http-equiv="X-UA-Compatible" content="IE=edge, chrome=1">
|
||||
<![endif]-->
|
||||
|
||||
<meta name="viewport" content="initial-scale=1.0, user-scalable=no">
|
||||
|
||||
<meta name="apple-mobile-web-app-capable" content="yes">
|
||||
<meta name="apple-mobile-web-app-status-bar-style" content="black">
|
||||
|
||||
<link rel="apple-touch-icon" href="Resources/icon.png">
|
||||
<link rel="apple-touch-startup-image" href="Resources/default.png">
|
||||
|
||||
<script src="Frameworks/Cup/Resources/js/jquery.min.js" type="text/javascript" charset="UTF-8"></script>
|
||||
<script src="Frameworks/Cup/Resources/js/vendor/jquery.ui.widget.js" type="text/javascript" charset="UTF-8"></script>
|
||||
<script src="Frameworks/Cup/Resources/js/jquery.iframe-transport.js" type="text/javascript" charset="UTF-8"></script>
|
||||
<script src="Frameworks/Cup/Resources/js/jquery.fileupload.js" type="text/javascript" charset="UTF-8"></script>
|
||||
|
||||
<title>UIBuilderDemo</title>
|
||||
|
||||
<!-- Custom javascript goes here -->
|
||||
<!-- End custom javascript -->
|
||||
|
||||
<script type="text/javascript">
|
||||
OBJJ_MAIN_FILE = "main.j";
|
||||
// The below will tell the compiler to not generate debug symbols but will generate type signatures and inline objj_msgSend functions.
|
||||
// This will affect only Objective-J code that is compiled when loading the application. It will not affect precompiled
|
||||
// code like the Cappuccino frameworks.
|
||||
// Uncomment or comment on the line below to change the flags
|
||||
OBJJ_COMPILER_FLAGS = ["IncludeDebugSymbols", "IncludeTypeSignatures", "SourceMap", "InlineMsgSend"];
|
||||
|
||||
var progressBar = null;
|
||||
|
||||
OBJJ_PROGRESS_CALLBACK = function(percent, appSize, path)
|
||||
{
|
||||
percent = percent * 100;
|
||||
|
||||
if (!progressBar)
|
||||
progressBar = document.getElementById("progress-bar");
|
||||
|
||||
if (progressBar)
|
||||
progressBar.style.width = Math.min(percent, 100) + "%";
|
||||
}
|
||||
|
||||
var loadingHTML =
|
||||
'<div id="loading">' +
|
||||
' <div id="loading-text">Loading...</div>' +
|
||||
' <div id="progress-indicator">' +
|
||||
' <span id="progress-bar" style="width:0%"></span>' +
|
||||
' </div>' +
|
||||
'</div>';
|
||||
</script>
|
||||
|
||||
<script type="text/javascript" src="Frameworks/Objective-J/Objective-J.js" charset="UTF-8"></script>
|
||||
|
||||
<style type="text/css">
|
||||
html, body, h1, p {
|
||||
margin: 0;
|
||||
padding: 0;
|
||||
}
|
||||
|
||||
/* We need a body wrapper because Cappuccino is unhappy if we change the body element */
|
||||
#cappuccino-body {
|
||||
/* Position it absolutely so it will fill the height without content */
|
||||
position: absolute;
|
||||
top: 0;
|
||||
bottom: 0;
|
||||
width: 100%;
|
||||
|
||||
/* Put it at the bottom of the stack so it doesn't interfere with UI */
|
||||
z-index: 0;
|
||||
}
|
||||
|
||||
#cappuccino-body .container {
|
||||
display: table;
|
||||
margin: 0 auto;
|
||||
height: 100%;
|
||||
}
|
||||
|
||||
#cappuccino-body .content {
|
||||
display: table-cell;
|
||||
height: 100%;
|
||||
vertical-align: top;
|
||||
}
|
||||
|
||||
#loading {
|
||||
position: relative;
|
||||
top: 35%;
|
||||
}
|
||||
|
||||
#loading-text {
|
||||
height: 1.5em;
|
||||
color: #555;
|
||||
font: normal bold 36px/36px Arial, sans-serif;
|
||||
}
|
||||
|
||||
#progress-indicator {
|
||||
padding: 0px;
|
||||
height: 16px;
|
||||
border: 5px solid #555;
|
||||
border-radius: 18px;
|
||||
background-color: white;
|
||||
}
|
||||
|
||||
#progress-bar {
|
||||
position: relative;
|
||||
top: -1px;
|
||||
left: -1px;
|
||||
display: block;
|
||||
height: 18px;
|
||||
|
||||
/* Compensate for moving the bar left 1px to overlap the indicator border */
|
||||
border-right: 1px solid #555;
|
||||
background-color: #555;
|
||||
}
|
||||
|
||||
#noscript {
|
||||
position: relative;
|
||||
top: 35%;
|
||||
padding: 1em 1.5em;
|
||||
border: 5px solid #555;
|
||||
border-radius: 16px;
|
||||
background-color: white;
|
||||
color: #555;
|
||||
text-align: center;
|
||||
font: bold 24px Arial, sans-serif;
|
||||
}
|
||||
|
||||
#noscript a {
|
||||
color: #98c0ff;
|
||||
text-decoration: none;
|
||||
}
|
||||
</style>
|
||||
</head>
|
||||
|
||||
<body>
|
||||
<div id="cappuccino-body">
|
||||
<div class="container">
|
||||
<div class="content">
|
||||
<script type="text/javascript">
|
||||
document.write(loadingHTML);
|
||||
</script>
|
||||
</div>
|
||||
</div>
|
||||
<noscript style="position:absolute; top:0; left:0; width:100%; height:100%">
|
||||
<div class="container">
|
||||
<div class="content">
|
||||
<div id="noscript">
|
||||
<p style="font-size:120%; margin-bottom:.75em">JavaScript is required for this application.</p>
|
||||
<p><a href="http://www.enable-javascript.com" target="_blank">Enable JavaScript</a></p>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</noscript>
|
||||
</div>
|
||||
</body>
|
||||
</html>
|
||||
Executable
+10
@@ -0,0 +1,10 @@
|
||||
@import <Foundation/Foundation.j>
|
||||
@import <AppKit/AppKit.j>
|
||||
|
||||
@import "UIElementView.j"
|
||||
@import "AppController.j"
|
||||
|
||||
function main(args, namedArgs)
|
||||
{
|
||||
CPApplicationMain();
|
||||
}
|
||||
Reference in new issue
Block a user