mirror of https://github.com/buggins/dlangui.git
WidgetGroup
This commit is contained in:
parent
72d698f72a
commit
fb1215502c
|
@ -2,6 +2,96 @@ module dlangui.widgets.controls;
|
||||||
|
|
||||||
import dlangui.widgets.widget;
|
import dlangui.widgets.widget;
|
||||||
|
|
||||||
|
/// static text widget
|
||||||
|
class TextWidget : Widget {
|
||||||
|
protected dstring _text;
|
||||||
|
/// get widget text
|
||||||
|
override @property dstring text() { return _text; }
|
||||||
|
/// set text to show
|
||||||
|
override @property void text(dstring s) {
|
||||||
|
_text = s;
|
||||||
|
requestLayout();
|
||||||
|
}
|
||||||
|
override void measure(int parentWidth, int parentHeight) {
|
||||||
|
_measuredWidth = _measuredHeight = 0;
|
||||||
|
FontRef font = font();
|
||||||
|
Point sz = font.textSize(text);
|
||||||
|
measuredContent(parentWidth, parentHeight, sz.x, sz.y);
|
||||||
|
}
|
||||||
|
override void onDraw(DrawBuf buf) {
|
||||||
|
if (visibility != Visibility.Visible)
|
||||||
|
return;
|
||||||
|
super.onDraw(buf);
|
||||||
|
Rect rc = _pos;
|
||||||
|
applyMargins(rc);
|
||||||
|
applyPadding(rc);
|
||||||
|
ClipRectSaver(buf, rc);
|
||||||
|
FontRef font = font();
|
||||||
|
Point sz = font.textSize(text);
|
||||||
|
applyAlign(rc, sz);
|
||||||
|
font.drawText(buf, rc.left, rc.top, text, textColor);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// image widget
|
||||||
|
class ImageWidget : Widget {
|
||||||
|
|
||||||
|
protected string _drawableId;
|
||||||
|
protected DrawableRef _drawable;
|
||||||
|
|
||||||
|
/// get drawable image id
|
||||||
|
@property string drawableId() { return _drawableId; }
|
||||||
|
/// set drawable image id
|
||||||
|
@property ImageWidget drawableId(string id) {
|
||||||
|
_drawableId = id;
|
||||||
|
_drawable.clear();
|
||||||
|
requestLayout();
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
/// get drawable
|
||||||
|
@property ref DrawableRef drawable() {
|
||||||
|
if (!_drawable.isNull)
|
||||||
|
return _drawable;
|
||||||
|
if (_drawableId !is null)
|
||||||
|
_drawable = drawableCache.get(_drawableId);
|
||||||
|
return _drawable;
|
||||||
|
}
|
||||||
|
/// set custom drawable (not one from resources)
|
||||||
|
@property ImageWidget drawable(DrawableRef img) {
|
||||||
|
_drawable = img;
|
||||||
|
_drawableId = null;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
override void measure(int parentWidth, int parentHeight) {
|
||||||
|
DrawableRef img = drawable;
|
||||||
|
int w = 0;
|
||||||
|
int h = 0;
|
||||||
|
if (!img.isNull) {
|
||||||
|
w = img.width;
|
||||||
|
h = img.height;
|
||||||
|
}
|
||||||
|
measuredContent(parentWidth, parentHeight, w, h);
|
||||||
|
}
|
||||||
|
override void onDraw(DrawBuf buf) {
|
||||||
|
if (visibility != Visibility.Visible)
|
||||||
|
return;
|
||||||
|
super.onDraw(buf);
|
||||||
|
Rect rc = _pos;
|
||||||
|
applyMargins(rc);
|
||||||
|
applyPadding(rc);
|
||||||
|
ClipRectSaver(buf, rc);
|
||||||
|
DrawableRef img = drawable;
|
||||||
|
if (!img.isNull) {
|
||||||
|
Point sz;
|
||||||
|
sz.x = img.width;
|
||||||
|
sz.y = img.height;
|
||||||
|
applyAlign(rc, sz);
|
||||||
|
img.drawTo(buf, rc);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
class Button : Widget {
|
class Button : Widget {
|
||||||
protected dstring _text;
|
protected dstring _text;
|
||||||
override @property dstring text() { return _text; }
|
override @property dstring text() { return _text; }
|
||||||
|
|
|
@ -252,19 +252,6 @@ class Widget {
|
||||||
DrawableRef bg = style.backgroundDrawable;
|
DrawableRef bg = style.backgroundDrawable;
|
||||||
bg.drawTo(buf, rc);
|
bg.drawTo(buf, rc);
|
||||||
applyPadding(rc);
|
applyPadding(rc);
|
||||||
/*
|
|
||||||
buf.fillRect(Rect(rc.left + rc.width / 2, rc.top, rc.left + rc.width / 2 + 2, rc.bottom), 0xFF8000);
|
|
||||||
buf.fillRect(Rect(rc.left, rc.top + rc.height / 2, rc.right, rc.top + rc.height / 2 + 2), 0xFF80FF);
|
|
||||||
DrawableRef img = drawableCache.get("exit");
|
|
||||||
if (!img.isNull)
|
|
||||||
img.drawTo(buf, Rect(50, 50, 50+64, 50+64));
|
|
||||||
img = drawableCache.get("btn_default_normal");
|
|
||||||
if (!img.isNull) {
|
|
||||||
img.drawTo(buf, Rect(150, 150, 150+100, 150+50));
|
|
||||||
img.drawTo(buf, Rect(250, 250, 250+60, 250+150));
|
|
||||||
img.drawTo(buf, Rect(50, 50, 50+30, 50+30));
|
|
||||||
}
|
|
||||||
*/
|
|
||||||
_needDraw = false;
|
_needDraw = false;
|
||||||
}
|
}
|
||||||
/// Applies alignment for content of size sz - set rectangle rc to aligned value of content inside of initial value of rc.
|
/// Applies alignment for content of size sz - set rectangle rc to aligned value of content inside of initial value of rc.
|
||||||
|
@ -298,6 +285,13 @@ class Widget {
|
||||||
@property int childCount() { return 0; }
|
@property int childCount() { return 0; }
|
||||||
/// returns child by index
|
/// returns child by index
|
||||||
Widget child(int index) { return null; }
|
Widget child(int index) { return null; }
|
||||||
|
/// adds child, returns added item
|
||||||
|
Widget addChild(Widget item) { assert(false, "children not suported for this widget type"); }
|
||||||
|
/// removes child, returns added item
|
||||||
|
Widget removeChild(int index) { assert(false, "children not suported for this widget type"); }
|
||||||
|
/// returns index of widget in child list, -1 if passed widget is not a child of this widget
|
||||||
|
int childIndex(Widget item) { return -1; }
|
||||||
|
|
||||||
/// find child by id, returns null if not found
|
/// find child by id, returns null if not found
|
||||||
Widget childById(string id) {
|
Widget childById(string id) {
|
||||||
if (compareId(id))
|
if (compareId(id))
|
||||||
|
@ -328,41 +322,68 @@ class Widget {
|
||||||
/// sets window (to be used for top level widget from Window implementation). TODO: hide it from API?
|
/// sets window (to be used for top level widget from Window implementation). TODO: hide it from API?
|
||||||
@property void window(Window window) { _window = window; }
|
@property void window(Window window) { _window = window; }
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// static text widget
|
/// widget list holder
|
||||||
class TextWidget : Widget {
|
struct WidgetList {
|
||||||
protected dstring _text;
|
protected Widget[] _list;
|
||||||
/// get widget text
|
protected int _count;
|
||||||
override @property dstring text() { return _text; }
|
/// returns count of items
|
||||||
/// set text to show
|
@property int count() { return _count; }
|
||||||
override @property void text(dstring s) {
|
/// get item by index
|
||||||
_text = s;
|
Widget get(int index) {
|
||||||
requestLayout();
|
assert(index >= 0 && index < _count, "child index out of range");
|
||||||
|
return _list[index];
|
||||||
}
|
}
|
||||||
override void measure(int parentWidth, int parentHeight) {
|
/// add item to list
|
||||||
_measuredWidth = _measuredHeight = 0;
|
Widget add(Widget item) {
|
||||||
FontRef font = font();
|
if (_list.length <= _count) // resize
|
||||||
Point sz = font.textSize(text);
|
_list.length = _list.length < 4 ? 4 : _list.length * 2;
|
||||||
measuredContent(parentWidth, parentHeight, sz.x, sz.y);
|
_list[_count++] = item;
|
||||||
|
return item;
|
||||||
}
|
}
|
||||||
override void layout(Rect rc) {
|
/// find child index for item, return -1 if not found
|
||||||
super.layout(rc);
|
int indexOf(Widget item) {
|
||||||
|
for (int i = 0; i < _count; i++)
|
||||||
|
if (_list[i] == item)
|
||||||
|
return i;
|
||||||
|
return -1;
|
||||||
}
|
}
|
||||||
override void onDraw(DrawBuf buf) {
|
/// remove item from list, return removed item
|
||||||
if (visibility != Visibility.Visible)
|
Widget remove(int index) {
|
||||||
return;
|
assert(index >= 0 && index < _count, "child index out of range");
|
||||||
super.onDraw(buf);
|
Widget item = _list[index];
|
||||||
Rect rc = _pos;
|
for (int i = index; i < _count - 1; i++)
|
||||||
applyMargins(rc);
|
_list[i] = _list[i + 1];
|
||||||
buf.fillRect(_pos, backgroundColor);
|
_count--;
|
||||||
applyPadding(rc);
|
return item;
|
||||||
ClipRectSaver(buf, rc);
|
}
|
||||||
FontRef font = font();
|
/// remove and destroy all items
|
||||||
Point sz = font.textSize(text);
|
void clear() {
|
||||||
applyAlign(rc, sz);
|
for (int i = 0; i < _count; i++) {
|
||||||
font.drawText(buf, rc.left, rc.top, text, textColor);
|
destroy(_list[i]);
|
||||||
|
_list[i] = null;
|
||||||
|
}
|
||||||
|
_count = 0;
|
||||||
|
}
|
||||||
|
~this() {
|
||||||
|
clear();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// base class for widgets which have children
|
||||||
|
class WidgetGroup : Widget {
|
||||||
|
|
||||||
|
protected WidgetList _children;
|
||||||
|
|
||||||
|
/// returns number of children of this widget
|
||||||
|
@property override int childCount() { return _children.count; }
|
||||||
|
/// returns child by index
|
||||||
|
override Widget child(int index) { return _children.get(index); }
|
||||||
|
/// adds child, returns added item
|
||||||
|
override Widget addChild(Widget item) { return _children.add(item); }
|
||||||
|
/// removes child, returns added item
|
||||||
|
override Widget removeChild(int index) { return _children.remove(index); }
|
||||||
|
/// returns index of widget in child list, -1 if passed widget is not a child of this widget
|
||||||
|
override int childIndex(Widget item) { return _children.indexOf(item); }
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue