緣起:
接受了一任務:要把設置了ClickStep的CEGUISlider的行為改成像wow里一樣,一次拖動一個step(當鼠標超過一半step的時候,Thumb跳到下一個節點)。
這個要改CEGUIThumb的onMouseMove。這次好好理解了下UDim,之前一直不肯花時間理解這個,這次不得不花精力了,懶是人的天性!理解了UDim這個問題就很簡單了!
http://blog.csdn.net/dizuo/archive/2009/03/21/4010718.aspx
CEGUI中的UDim
今天看了CEGUI中最新增加的The Unified Co-ordinate System。覺得很有創意啊。真佩服設計者。
一直以來窗口坐標系中坐標轉換就令人頭暈,相對坐標系,絕對坐標系。
1,相對坐標系,主要是當窗口size改變以后,object在窗口的相對位置不變,因此必須使用比例系數來設定。
例如opengl中的reshape函數中glPerspective中aspect量的設置,為了窗口變化時候,視口不變,aspect = w/h。而不是使用絕對的數字,0.5,或其他。
2,絕對坐標系,就是窗口坐標系(一般是X軸向右為正,Y向下為正,原點左上角)中的坐標,窗口變化后,坐標不變。
然而CEGUI中的UDim設計正是結合了相對量和絕對量,UDim成員:
scale:相對于父窗口的縮放參數,介于0到1之間。是相對量
offset是偏移量,以像素為單位,有正負之分。源文件定義為float類型。是絕對量。
一般寫法:
- UDim(float scale, float offset)
可以利用下面的宏單獨使用UDim表示絕對量,或者相對量。
- #define cegui_reldim(x) CEGUI::UDim((x),0) //相對量定義
- #define cegui_absdim(x) CEGUI::UDim(0,(x)) //絕對量的定義
舉個例子:
在sample中的FirstWindow例子中,窗口設置語句:
- // Windows are in Relative metrics mode by default. This means that we can
- // specify sizes and positions without having to know the exact pixel size
- // of the elements in advance. The relative metrics mode co-ordinates are
- // relative to the parent of the window where the co-ordinates are being set.
- // This means that if 0.5f is specified as the width for a window, that window
- // will be half as its parent window.
- //
- // Here we set the FrameWindow so that it is half the size of the display,
- // and centered within the display.
- wnd->setPosition(UVector2(cegui_reldim(0.25f), cegui_reldim( 0.25f)));
- wnd->setSize(UVector2(cegui_reldim(0.5f), cegui_reldim( 0.5f)));
- // now we set the maximum and minum sizes for the new window. These are
- // specified using relative co-ordinates, but the important thing to note
- // is that these settings are aways relative to the display rather than the
- // parent window.
- //
- // here we set a maximum size for the FrameWindow which is equal to the size
- // of the display, and a minimum size of one tenth of the display.
- wnd->setMaxSize(UVector2(cegui_reldim(1.0f), cegui_reldim( 1.0f)));
- wnd->setMinSize(UVector2(cegui_reldim(0.1f), cegui_reldim( 0.1f)));
四個語句使用了相對量cegui_reldim設置了窗口的屬性,w,h為父窗口尺寸
位置:(w/4, h/4)
尺寸:width = w/2 height = h/2
最大尺寸:w_max = w,h_max = h
最小尺寸:w_min = w/10, h_min = h/10
而運行效果正是預期。
UDim既然是一維的,可以表示width,height,xposition,yposition等。
CEGUI主要是做GUI的,所以統一坐標系下只給出了一維的UDim,二維的UVector,以及平面中的URect定義。理解了一維的東西,二維UVector,URect就很好理解了。
http://blog.csdn.net/bluekane/archive/2009/01/09/3738482.aspx
cegui 6
統一度量系統共有三種形式
* UDim : 簡單的一維 * UVector2 : 由兩個UDim組成的二維向量 * URect : 用四個UDim表示一個矩形,依次為:左,上,右,下
- 格式為 :{scale, offset}
- 例如 :{1,0}
另一個例子:
{0.5, 25}這將使得到的窗口寬度為其父窗口的一半加上25像素。
使用單一UDim作為其值的屬性有:
* UnifiedXPosition * UnifiedYPosition * UnifiedWidth * UnifiedHeight
- 格式為 :{{x-scale, x-offset}, {y-scale, y-offset}}
- 例如 :{{1, 0}, {1, 0}}
- {{1,0},{0,100}}
使用Uvector2作為其值的屬性有:
* UnifiedPosition * UnifiedSize * UnifiedMinSize * UnifiedMaxSize
- 格式為 :{{ls,lo},{ts,to},{rs,ro},{bs,bo}}
- 例如 :{{0,0},{0,0},{1,0},{1,0}}
我們定義矩形區域而不是其大小的做法是很聰明的。比如:假如我們想使一個窗口覆蓋它的父窗口,但要為父窗口的四邊分別留出10像素的大小,代碼可以這樣寫:
- {{0,10},{0,10},{1,-10},{1,-10}}
- <property name="UnifiedPosition" value="{{0.1,10},{1.0,-30}}">
- </property>
Y-position:父窗口的高度 - 30像素
- <property name="UnifiedSize" value="{{0.6,5},{0.3,20}}">
- </property>
Height:父窗口高度的30% + 20像素
- <property name="UnifiedXPosition" value="{0.25,-5}">
- </property>
- <property name="UnifiedAreaRect" value="{{0.1,0},{0.1,0},{0.9,0},{0.9,0}}">
- </property>
Y-position:父窗口高度的10%
Width:父窗口寬度的80%
Height:父窗口高度的80%