# HG changeset patch # User Diggory Hardy # Date 1230821529 0 # Node ID d28aea50c6da4f2cfb47fbb0866189a74d6430c8 # Parent 9ac208b53582f404ece6589df6d54e108aa8721c Basic edit cursor placement using the mouse. Moved textContent.d's contents into TextWidget.d. diff -r 9ac208b53582 -r d28aea50c6da mde/content/AStringContent.d --- a/mde/content/AStringContent.d Sun Dec 28 10:55:15 2008 +0000 +++ b/mde/content/AStringContent.d Thu Jan 01 14:52:09 2009 +0000 @@ -115,7 +115,9 @@ return sv; } - /// Get the character the edit cursor is in front of + /** Get the character the edit cursor is in front of. + * + * NOTE: unused. */ size_t editIndex () { size_t i = 0; for (size_t p = 0; p < pos; ++p) @@ -123,6 +125,17 @@ ++i; return i; } + /** Set the edit position in front of i'th character. + * + * Assumes at least i characters are present; if not this will work but not be optimal. */ + void editIndex (size_t i) { + pos = 0; + for (; i > 0; --i) { // NOTE: could be slightly optimised + if (pos < sv.length) ++pos; + while (pos < sv.length && (sv[pos] & 0x80) && !(sv[pos] & 0x40)) + ++pos; + } + } /** Call after editing a string; return new string (may be changed/reverted). */ char[] endEdit (); diff -r 9ac208b53582 -r d28aea50c6da mde/gui/renderer/IRenderer.d --- a/mde/gui/renderer/IRenderer.d Sun Dec 28 10:55:15 2008 +0000 +++ b/mde/gui/renderer/IRenderer.d Thu Jan 01 14:52:09 2009 +0000 @@ -103,6 +103,20 @@ void index (size_t index = size_t.max) { index_ = index; } + /** Set the edit index for a click at x. + * + * TODO: this uses the left edge of the cached bitmap to compare against; it should + * probably use the glyph position. The font system needs some redesigning anyway. */ + size_t setIndex (wdrel x) { + foreach (i,ch; textCache.chars) + if (x < ch.xPos) { + index_ = i; + goto gotIndex; + } + index_ = textCache.chars.length; + gotIndex: + return index_; + } void getDimensions (out wdsize w, out wdsize h) { font.updateBlock (content, textCache); diff -r 9ac208b53582 -r d28aea50c6da mde/gui/widget/PopupMenu.d --- a/mde/gui/widget/PopupMenu.d Sun Dec 28 10:55:15 2008 +0000 +++ b/mde/gui/widget/PopupMenu.d Thu Jan 01 14:52:09 2009 +0000 @@ -19,7 +19,6 @@ module mde.gui.widget.PopupMenu; import mde.gui.widget.Widget; -import mde.gui.widget.textContent; import mde.gui.widget.TextWidget; import mde.gui.widget.layout; diff -r 9ac208b53582 -r d28aea50c6da mde/gui/widget/TextWidget.d --- a/mde/gui/widget/TextWidget.d Sun Dec 28 10:55:15 2008 +0000 +++ b/mde/gui/widget/TextWidget.d Thu Jan 01 14:52:09 2009 +0000 @@ -13,16 +13,13 @@ You should have received a copy of the GNU General Public License along with this program. If not, see . */ -/** Basic text widget and supporting code for widgets containing text. - * - * All content widgets have one (at least for basic content widgets) Content, from - * mde.gui.content.Items . */ +/** Simple text widgets. */ module mde.gui.widget.TextWidget; import mde.gui.widget.Widget; import mde.gui.exception; import mde.gui.renderer.IRenderer; -import mde.content.Content; +import mde.content.AStringContent; import tango.util.log.Log : Log, Logger; private Logger logger; @@ -30,14 +27,12 @@ logger = Log.getLogger ("mde.gui.widget.TextWidget"); } -/** Base text widget. - * - * Little use currently except to ease future additions. */ +/** Base text widget. */ class ATextWidget : AWidget { /** Set the adapter first: - * adapter = mgr.renderer.getAdapter ("string", 0xRRGGBB); */ - this (IWidgetManager mgr, widgetID id, WidgetData data) { + * adapter = mgr.renderer.getAdapter (...); */ + protected this (IWidgetManager mgr, widgetID id, WidgetData data) { super (mgr, id, data); } @@ -64,7 +59,9 @@ } -/// Basic text widget +/** Basic text widget + * + * Displays data.strings[0] directly (no translation). */ class TextLabelWidget : ATextWidget { /** Constructor for a widget containing [fixed] content. @@ -81,7 +78,10 @@ } } -/// Basic widget displaying a label from a content. +/** Basic widget displaying a label from a content. + * + * Can display value, name, description or possibly other field of a content, dependent on + * data.ints[1]. */ class ContentLabelWidget : ATextWidget { this (IWidgetManager mgr, widgetID id, WidgetData data, IContent c) { @@ -102,3 +102,56 @@ IContent content; int index; } + +/// Just displays the value of a content. Generic − any IContent. +class DisplayContentWidget : ATextWidget +{ + this (IWidgetManager mgr, widgetID id, WidgetData data, IContent c) { + content = c; + WDCMinCheck(data, 1,0, content); + adapter = mgr.renderer.getAdapter (); + adapter.text = content.toString(0); + super (mgr, id, data); + } + +protected: + IContent content; +} + +/// Text-box for editing a content's value. Generic − any AStringContent. +class AStringContentWidget : ATextWidget +{ + this (IWidgetManager mgr, widgetID id, WidgetData data, IContent c) { + content = cast(AStringContent) c; + WDCMinCheck(data, 1,0, content); + adapter = mgr.renderer.getAdapter (); + adapter.text = content.toString(0); + super (mgr, id, data); + } + + override bool isWSizable () { return true; } + override bool isHSizable () { return true; } + + /** On click, request keyboard input. */ + override int clickEvent (wdabs cx, wdabs, ubyte, bool) { + //adapter.index = content.editIndex; + content.editIndex = adapter.setIndex (cx - x); + mgr.requestRedraw; + return 1; // get keyboard input via keyEvent + } + + override void keyEvent (ushort s, char[] i) { + adapter.text = content.keyStroke (s, i); + adapter.index = content.editIndex; + adapter.getDimensions (mw, mh); // NOTE: only passively change size: next resize will see new minimal size + mgr.requestRedraw; + } + override void keyFocusLost () { + adapter.text = content.endEdit; // update other users of content relying on callbacks + adapter.index; + mgr.requestRedraw; + } + +protected: + AStringContent content; +} diff -r 9ac208b53582 -r d28aea50c6da mde/gui/widget/createWidget.d --- a/mde/gui/widget/createWidget.d Sun Dec 28 10:55:15 2008 +0000 +++ b/mde/gui/widget/createWidget.d Thu Jan 01 14:52:09 2009 +0000 @@ -29,7 +29,6 @@ import mde.gui.widget.miscWidgets; import mde.gui.widget.TextWidget; import mde.gui.widget.miscContent; -import mde.gui.widget.textContent; import mde.gui.widget.Floating; import mde.gui.widget.PopupMenu; diff -r 9ac208b53582 -r d28aea50c6da mde/gui/widget/miscContent.d --- a/mde/gui/widget/miscContent.d Sun Dec 28 10:55:15 2008 +0000 +++ b/mde/gui/widget/miscContent.d Thu Jan 01 14:52:09 2009 +0000 @@ -20,7 +20,7 @@ import mde.gui.widget.Widget; import mde.gui.exception; -import mde.gui.widget.textContent; +import mde.gui.widget.TextWidget; import mde.gui.widget.layout; import mde.gui.widget.PopupMenu; diff -r 9ac208b53582 -r d28aea50c6da mde/gui/widget/textContent.d --- a/mde/gui/widget/textContent.d Sun Dec 28 10:55:15 2008 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,84 +0,0 @@ -/* LICENSE BLOCK -Part of mde: a Modular D game-oriented Engine -Copyright © 2007-2008 Diggory Hardy - -This program is free software: you can redistribute it and/or modify it under the terms -of the GNU General Public License as published by the Free Software Foundation, either -version 2 of the License, or (at your option) any later version. - -This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; -without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. -See the GNU General Public License for more details. - -You should have received a copy of the GNU General Public License -along with this program. If not, see . */ - -/** Content widgets based on a text block. */ -module textContent; - -import mde.gui.widget.Widget; -import mde.gui.widget.TextWidget; -import mde.gui.exception; -import mde.gui.renderer.IRenderer; - -import mde.content.AStringContent; - -debug { - import tango.util.log.Log : Log, Logger; - private Logger logger; - static this () { - logger = Log.getLogger ("mde.gui.widget.textContent"); - } -} - -/// Just displays the content. Generic − any IContent. -class DisplayContentWidget : ATextWidget -{ - this (IWidgetManager mgr, widgetID id, WidgetData data, IContent c) { - content = c; - WDCMinCheck(data, 1,0, content); - adapter = mgr.renderer.getAdapter (); - adapter.text = content.toString(0); - super (mgr, id, data); - } - -protected: - IContent content; -} - -/// Text-box for editing a content. Generic − any AStringContent. -class AStringContentWidget : ATextWidget -{ - this (IWidgetManager mgr, widgetID id, WidgetData data, IContent c) { - content = cast(AStringContent) c; - WDCMinCheck(data, 1,0, content); - adapter = mgr.renderer.getAdapter (); - adapter.text = content.toString(0); - super (mgr, id, data); - } - - override bool isWSizable () { return true; } - override bool isHSizable () { return true; } - - /** On click, request keyboard input. */ - override int clickEvent (wdabs, wdabs, ubyte, bool state) { - adapter.index = content.editIndex; - mgr.requestRedraw; - return 1; // get keyboard input via keyEvent - } - - override void keyEvent (ushort s, char[] i) { - adapter.text = content.keyStroke (s, i); - adapter.index = content.editIndex; - adapter.getDimensions (mw, mh); // NOTE: only passively change size: next resize will see new minimal size - mgr.requestRedraw; - } - override void keyFocusLost () { - adapter.text = content.endEdit; // update other users of content relying on callbacks - adapter.index; - mgr.requestRedraw; - } - -protected: - AStringContent content; -}