/libs/gui/hug/HUGListBox.hx
http://na-agent.googlecode.com/ · Haxe · 408 lines · 303 code · 69 blank · 36 comment · 53 complexity · 846f7e934c201d52a70272c411fab9fb MD5 · raw file
- package gui.hug;
-
- import flash.display.DisplayObject;
- import flash.display.DisplayObjectContainer;
- import flash.display.Sprite;
- import flash.display.Stage;
- import flash.events.KeyboardEvent;
- import flash.events.MouseEvent;
- import flash.events.Event;
- import flash.display.MovieClip;
- import flash.text.TextField;
- import gui.hug.priv.HUGBorder;
-
- /**
- * @author Alexey Kharkov
- */
- class HUGListBox extends HUGSprite
- {
- private var items:Array<Dynamic>; //private var items:Array<HUGComboItem>;
-
-
- private var cur:HUGComboItem; // Currently active item
- private var hl:HUGComboItem; // Highlighted item
- private var w:Float; // width
-
- private var sel:HUGSprite; // Highlighting rectangle
- private var sb:HUGScrollBar;
-
- public var owner:HUGComboBox;
- private var enMouse:Bool;
-
- static public inline var ITEM_H:UInt = 19;
- static public inline var W:UInt = 16; // ScrollBar width
- static public inline var ITEMS_COUNT_TO_SCROLL:UInt = 12;
-
-
- public function new( x:Int, y:Int, w:Int )
- {
- super();
- this.x = x;
- this.y = y;
- this.w = w;
-
- enMouse = true;
-
- items = new Array();
-
- //Dbg.init( this );
-
- //mouseEnabled = false;
- buttonMode = true;
-
- // Selection rects
- sel = new HUGSprite();
- Utils.rect( sel, 0, 1, w, ITEM_H - 1, Utils.SEL_BG_COL, Utils.SEL_BG_BORDER_COL );
- //drawSel( w );
- sel.mouseEnabled = false;
- addChild( sel );
- sel.mask = maskRect();
-
- // Scroll bar
- sb = new HUGScrollBar( w - W, 0, ITEM_H * ITEMS_COUNT_TO_SCROLL - 1 );
- sb.visible = false;
- sb.addEventListener( Event.SCROLL, onScroll );
- addChild( sb );
-
- // Keys control
- addEventListener( KeyboardEvent.KEY_DOWN, onKeyDown );
-
- //
- drawBg();
-
- addEventListener( MouseEvent.ROLL_OVER, onOver );
- addEventListener( MouseEvent.ROLL_OUT, onOut );
- addEventListener( MouseEvent.MOUSE_WHEEL, onWheel );
- }
-
- public function clear()
- {
- cur = null;
- items = new Array();
- clearLayout();
-
- // Reset Selection Rect
- //drawSel( w );
-
- // Reset ScrollBar
- sb.init( 0, ITEMS_COUNT_TO_SCROLL );
- sb.setScrollPosition(0);
- sb.visible = false;
- }
-
- public function addItemsArray( arr:Array<Dynamic> )
- {
- for (i in 0...arr.length)
- addItemHelper( arr[i] );
-
- upd();
- }
-
- public function addItem( s:String )
- {
- addItemHelper( s );
- upd();
- }
-
- public function replaceItemAt( key:Int, s:String )
- {
- if(key < getLength()) {
- var idx:Int = key;
- var item: HUGComboItem = new HUGComboItem( this, s, idx, w );
- item.y = idx * ITEM_H - 1;
-
- if ( idx == 0 )
- {
- cur = item;
- hl = item;
- setItemActive( item );
- }
-
- items[key] = item;
- reDraw();
- }
- }
-
- public function getSelectedIndex():Int
- {
- return (cur != null) ? cur.idx : 0;
- }
-
- /*public function getItem(index : Int) : String
- {
- return items[index].txt.text;
- }
- */
-
- public function setSelectedIndex( idx:Int )
- {
- if ( idx >= 0 && idx < getLength() )
- {
- onItemClick( items[idx], false );
- scrollToCur();
- }
- }
-
- public function getLength():Int
- {
- return items.length;
- }
-
- public function getWidth(): Float
- {
- return w;
- }
-
- public function getHeight():Float
- {
- return Math.min(getLength(), ITEMS_COUNT_TO_SCROLL) * ITEM_H;
- }
-
- // ---------------------------------------------------------------------------- internal methods.
- public function setItemActive( item: Dynamic )
- {
- if ( hl != null )
- hl.txt.textColor = 0;
-
- hl = item;
- sel.y = item.y;
- item.txt.textColor = 0xffffff;
- }
-
- public function onItemClick( item: Dynamic, b:Bool )
- {
- cur = item;
- setItemActive( item );
-
- if ( owner == null )
- dispatchEvent( new Event( Event.CHANGE ) );
- else
- owner.onItemClick( item, b );
- }
-
- public function reset()
- {
- setItemActive( cur );
- scrollToCur();
- }
-
- public function getSelY():Float
- {
- return sel.y;
- }
-
- // ---------------------------------------------------------------------------- private methods.
- private function addItemHelper( s:String )
- {
- var idx:Int = getLength();
- var item: HUGComboItem = new HUGComboItem( this, s, idx, w );
- item.y = idx * ITEM_H - 1;
-
- if ( idx == 0 )
- {
- cur = item;
- hl = item;
- setItemActive( item );
- }
-
- items.push( item );
- }
-
- private function upd()
- {
- drawBg();
-
- if ( getLength() > ITEMS_COUNT_TO_SCROLL )
- {
- sb.init( getLength() - ITEMS_COUNT_TO_SCROLL, ITEMS_COUNT_TO_SCROLL );
- sb.setScrollPosition(0);
-
- sb.visible = true;
-
- //if ( getLength() == 1 + ITEMS_COUNT_TO_SCROLL )
- //{
- //drawSel( w - W - 1 );
- //}
- } else
- reDraw();
- }
-
- private function scrollToCur()
- {
- if ( cur.y < 0 || cur.y >= ITEMS_COUNT_TO_SCROLL * ITEM_H - 1 )
- {
- sb.setScrollPosition(cur.idx - ITEMS_COUNT_TO_SCROLL / 3);
- reDraw();
- }
- }
-
- private function scrollToBounds( item:HUGComboItem )
- {
- if ( item.y < 0 )
- {
- sb.setScrollPosition(item.idx);
- } else
- if ( item.y >= ITEMS_COUNT_TO_SCROLL * ITEM_H - 1 )
- {
- sb.setScrollPosition(item.idx - ITEMS_COUNT_TO_SCROLL + 1);
- }
- reDraw();
- }
-
- private function maskRect():HUGSprite
- {
- var ss:HUGSprite = new HUGSprite();
- Utils.fillRect( ss, 0, 0, w + 1, ITEMS_COUNT_TO_SCROLL * ITEM_H + 1, 0 );
- addChild( ss );
- return ss;
- }
-
- private function drawBg()
- {
- graphics.clear();
- var h:Float = Math.min(getLength(), ITEMS_COUNT_TO_SCROLL) * ITEM_H - 1;
- Utils.rect( this, 0, 0, w, h, 0xffffff, 0xcccccc, (owner == null) ? 1 : 0.92 );
-
- if ( owner != null )
- {
- Utils.horLine( this, 0, w, 0, 0xadadad );
- Utils.horLine( this, 0, w, h, 0xadadad, 0.95 );
- Utils.horLine( this, 0, w, h + 1, 0xadadad, 0.5 );
- Utils.horLine( this, 0, w, h + 2, 0xadadad, 0.2 );
- }
- }
-
- //private function drawSel( ww:uint ):void
- //{
- // sel.graphics.clear();
- // Utils.rect( sel, 0, 1, ww, ITEM_H - 1, Utils.SEL_BG_COL, Utils.SEL_BG_BORDER_COL );
- //}
-
- private function reDraw()
- {
- //removeEventListener( MouseEvent.ROLL_OVER, onOver );
- //removeEventListener( MouseEvent.ROLL_OUT, onOut );
-
- clearLayout();
-
- var yy:UInt = -ITEM_H * Math.round( sb.getScrollPosition() );
- for (i in 0...items.length)
- {
- items[i].y = yy - 1;
- yy += ITEM_H;
-
- if ( items[i].y > -ITEM_H && items[i].y < height )
- {
- addChild( items[i] );
- items[i].mask = maskRect();
- }
- }
-
- setChildIndex( sb, numChildren - 1 );
- setItemActive( hl );
-
- //addEventListener( MouseEvent.ROLL_OVER, onOver );
- //addEventListener( MouseEvent.ROLL_OUT, onOut );
- }
-
- private function clearLayout()
- {
- while ( numChildren > 2 ) // the first two children are "sel" and "sel mask"
- removeChildAt( numChildren - 1 );
- addChild( sb );
- }
-
- private function mouseInside():Bool
- {
- return mouseX > 0 && mouseX < width && mouseY > 0 && mouseY < height;
- }
-
- // ----------------------------------------------------------------------- Event handlers
- private function onKeyDown( e:KeyboardEvent )
- {
- if ( owner != null )
- return;
-
- enMouse = false;
- if ( e.keyCode == 38 )
- {
- if ( getSelectedIndex() > 0 )
- {
- onItemClick( items[getSelectedIndex() - 1], false );
- scrollToBounds( cur );
- hl.graphics.clear();
- }
- } else
- if ( e.keyCode == 40 )
- {
- if ( getSelectedIndex() < getLength() - 1 )
- {
- onItemClick( items[getSelectedIndex() + 1], false );
- scrollToBounds( cur );
- hl.graphics.clear();
- }
- }
- addEventListener( MouseEvent.MOUSE_MOVE, helpListener );
- }
-
-
- public function onComboKeyDown( e:KeyboardEvent )
- {
- enMouse = false;
- if ( e.keyCode == 13 )
- {
- onItemClick( hl, true );
- } else
- if ( e.keyCode == 38 )
- {
- if ( hl.idx > 0 )
- {
- setItemActive( items[hl.idx - 1] );
- scrollToBounds( hl );
- }
- } else
- if ( e.keyCode == 40 )
- {
- if ( hl.idx < getLength() - 1 )
- {
- setItemActive( items[hl.idx + 1] );
- scrollToBounds( hl );
- }
- }
- addEventListener( MouseEvent.MOUSE_MOVE, helpListener );
- }
-
- private function helpListener(e: MouseEvent) {
- enMouse = true;
- }
-
- private function onOver( e:MouseEvent )
- {
- //Dbg.log( "onOver( " + mouseX + ", " + mouseY + " ), " + x + ", " + y + " - " + (x+width) + ", " + (y+height) + " hitTest " + mouseInside() );
- if ( e.target == this && owner == null )
- {
- MouseWheel.capture();
- }
- }
-
- private function onOut( e:MouseEvent )
- {
- //Dbg.log( "onOut( " + mouseX + ", " + mouseY + " ), " + x + ", " + y + " - " + (x+width) + ", " + (y+height) + " hitTest " + mouseInside() );
- if ( e.target == this && owner == null && !mouseInside() )
- {
- MouseWheel.release();
- }
- }
-
- private function onScroll( e:Event )
- {
- reDraw();
- }
-
- private function onWheel( e:MouseEvent )
- {
- enMouse = true;
- sb.setScrollPosition(sb.getScrollPosition() - e.delta);
- reDraw();
- }
- }