Files
kiss-flixel/src/kiss_flixel/SimpleWindow.kiss

118 lines
3.8 KiB
Plaintext

// All windows share the same text size
(var &mut textSize 16)
(var :kiss.List<SimpleWindow> windowStack [])
(prop &mut keyboardEnabled true)
// TODO tooltip support with left-click and right-click action
// icons and explanations
(defNew [&opt :String _title
:FlxColor bgColor
:FlxColor _textColor
:Float percentWidth
:Float percentHeight]
[:String title (or _title "")
&mut :Float nextControlY 0
:FlxColor titleColor (or _textColor FlxColor.WHITE)
&mut :FlxColor textColor (or _textColor FlxColor.WHITE)
:FlxTypedGroup<FlxSprite> controls (new FlxTypedGroup)
:FlxKeyShortcutHandler<ShortcutAction> keyHandler (new FlxKeyShortcutHandler)]
(super 0 0)
(makeGraphic
(Std.int (* FlxG.width (or percentWidth 0.5)))
(Std.int (* FlxG.height (or percentHeight 0.5)))
(or bgColor FlxColor.BLACK))
(screenCenter)
(set nextControlY y)
(when title
(makeText title null))
(set keyHandler.onBadKey ->:Void [_ _] {}) // TODO do SOMETHING
(set keyHandler.onSelectItem
->:Void [:ShortcutAction a] {
(a)
(keyHandler.start)
})
// TODO show which shortcuts' prefixes are partially highlighted?
)
(prop :Map<FlxSprite,Action> _actions (new Map))
(prop :Map<FlxSprite,FlxColor> _colors (new Map))
(method makeText [:String text &opt :FlxColor color :Action onClick]
(let [ftext (new FlxText x nextControlY 0 text textSize)]
(set ftext.color (or color textColor))
(dictSet _colors ftext ftext.color)
(set ftext.cameras this.cameras)
(controls.add ftext)
(+= nextControlY ftext.height)
(when onClick
(dictSet _actions ftext onClick)
// TODO right click?
(keyHandler.registerItem text ->:Void (onClick ftext)))
ftext))
// TODO makeButton
// TODO make inputText
(prop &mut _shown false)
(method isShown [] _shown)
(method clearControls []
(_actions.clear)
(controls.clear)
(keyHandler.clear)
(set nextControlY y)
(makeText title titleColor))
(method :Void show []
(unless _shown
(FlxG.state.add this)
(FlxG.state.add controls)
(windowStack.push this)
(keyHandler.start)
(set _shown true)))
(method :Void hide []
(when _shown
(FlxG.state.remove this)
(FlxG.state.remove controls)
(windowStack.remove this)
(keyHandler.cancel)
(set _shown false)))
(function getHighlighted [:FlxColor color &opt :Float amount]
(unless amount (set amount 0.2))
(cond ((> color.lightness amount)
(color.getDarkened amount))
(true
(color.getLightened amount))))
(prop &mut otherIsSelected false)
(method &override update [:Float elapsed]
(super.update elapsed)
(set otherIsSelected false)
(when (= (last windowStack) this)
(when keyboardEnabled
(keyHandler.update))
// Handle mouse input
(let [camera (first (or cameras []))
mousePos (FlxG.mouse.getScreenPosition camera)]
(controls.forEach ->text
(whenLet [onClick (dictGet _actions text)]
(let [color (dictGet _colors text)]
(if (and !otherIsSelected (.containsPoint (text.getScreenBounds camera) mousePos))
{
(when FlxG.mouse.justPressed
(onClick text))
(set otherIsSelected true)
(set text.color (getHighlighted color))
}
{
(set text.color color)
})))))))