texturesselector.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471
  1. #ifndef _XBOX
  2. #include "texturesselector.h"
  3. #include "..\..\..\common_h\particles.h"
  4. #include "strutil.h"
  5. //extern IParticleManager* pPS;
  6. TTextureSelector::TTextureSelector () : GUIWindow (NULL, 100, 100, 800, 600), Frames(_FL_)
  7. {
  8. bMovable = false;
  9. bSystemButton = false;
  10. GridLineColor = 0xFF805959;
  11. SelectionColor = 0xFF00FF71;
  12. pFont->SetName("arialcyrsmall");
  13. OffsetX = 0;
  14. OffsetY = 0;
  15. Scale = 1.0f;
  16. GridMode = 32.0f;
  17. GlobalTexture = NEW GUIImage();
  18. Caption = "Texture selector";
  19. SetScreenCenter();
  20. Panel1 = NEW TTexPanel (this, 5, 5, 512, 512);
  21. Panel1->OnBeforeDraw = (CONTROL_EVENT)&TTextureSelector::RenderViewport;
  22. Panel1->LMB_Pressed = (CONTROL_EVENT)&TTextureSelector::SelectFrame;
  23. ColorDesc = NEW GUILabel (this, 530, 8, 52, 18);
  24. ColorDesc->Layout = GUILABELLAYOUT_Left;
  25. ColorDesc->Caption = "Lines color";
  26. ColorDesc->pFont->SetName("arialcyrsmall");
  27. ColorDesc2 = NEW GUILabel (this, 620, 8, 52, 18);
  28. ColorDesc2->Layout = GUILABELLAYOUT_Left;
  29. ColorDesc2->Caption = "Selection color";
  30. ColorDesc2->pFont->SetName("arialcyrsmall");
  31. GridColor = NEW GUIPanel (this, 530, 25, 62, 18);
  32. GridColor->OnBeforeDraw = (CONTROL_EVENT)&TTextureSelector::RenderGridColor;
  33. GridColor->OnMouseDown = (CONTROL_EVENT)&TTextureSelector::SelectColorDialog;
  34. SelectColor = NEW GUIPanel (this, 630, 25, 62, 18);
  35. SelectColor->OnBeforeDraw = (CONTROL_EVENT)&TTextureSelector::RenderSelColor;
  36. SelectColor->OnMouseDown = (CONTROL_EVENT)&TTextureSelector::SelectColorDialog2;
  37. lstFrames = NEW GUIListBox (this, 533, 100, 250, 415);
  38. lstFrames->MakeFlat(true);
  39. btnAddFrame = NEW GUIButton (this, 533, 70, 24, 24);
  40. btnAddFrame->Hint = "Add frame";
  41. btnAddFrame->OnMousePressed = (CONTROL_EVENT)&TTextureSelector::btnAddFramePressed;
  42. btnAddFrame->Glyph->Load("peditor\\addsystem");
  43. btnAddFrame->FlatButton = true;
  44. btnRemoveFrame = NEW GUIButton (this, 533+30, 70, 24, 24);
  45. btnRemoveFrame->Hint = "Remove frame";
  46. btnRemoveFrame->OnMousePressed = (CONTROL_EVENT)&TTextureSelector::btnRemoveFramePressed;
  47. btnRemoveFrame->Glyph->Load("peditor\\delsystem");
  48. btnRemoveFrame->FlatButton = true;
  49. btnClose = NEW GUIButton (this, 713, 530, 56, 28);
  50. btnClose->Hint = "Close window";
  51. btnClose->Caption = "Close";
  52. btnClose->pFont->SetName("arialcyrsmall");
  53. btnClose->FlatButton = true;
  54. btnClose->OnMousePressed = (CONTROL_EVENT)&TTextureSelector::Close;
  55. btnSize16 = NEW GUIButton (this, 25, 530, 48, 24);
  56. btnSize16->Hint = "Set size 16x16";
  57. btnSize16->FlatButton = true;
  58. btnSize16->GroupIndex = 1;
  59. btnSize16->Glyph->Load("peditor\\16x16");
  60. btnSize16->OnMousePressed = (CONTROL_EVENT)&TTextureSelector::btn16Pressed;
  61. btnSize32 = NEW GUIButton (this, 25+(48*1)+(10*1), 530, 48, 24);
  62. btnSize32->Hint = "Set size 32x32";
  63. btnSize32->FlatButton = true;
  64. btnSize32->GroupIndex = 1;
  65. btnSize32->Glyph->Load("peditor\\32x32");
  66. btnSize32->OnMousePressed = (CONTROL_EVENT)&TTextureSelector::btn32Pressed;
  67. btnSize32->Down = true;
  68. btnSize64 = NEW GUIButton (this, 25+(48*2)+(10*2), 530, 48, 24);;
  69. btnSize64->Hint = "Set size 64x64";
  70. btnSize64->FlatButton = true;
  71. btnSize64->GroupIndex = 1;
  72. btnSize64->Glyph->Load("peditor\\64x64");
  73. btnSize64->OnMousePressed = (CONTROL_EVENT)&TTextureSelector::btn64Pressed;
  74. btnSize128 = NEW GUIButton (this, 25+(48*3)+(10*3), 530, 48, 24);
  75. btnSize128->Hint = "Set size 128x128";
  76. btnSize128->FlatButton = true;
  77. btnSize128->GroupIndex = 1;
  78. btnSize128->Glyph->Load("peditor\\128x128");
  79. btnSize128->OnMousePressed = (CONTROL_EVENT)&TTextureSelector::btn128Pressed;
  80. }
  81. TTextureSelector::~TTextureSelector ()
  82. {
  83. delete GlobalTexture;
  84. }
  85. void _cdecl TTextureSelector::RenderViewport (GUIControl* sender)
  86. {
  87. int index = lstFrames->SelectedLine;
  88. Caption.Format("Texture selector (zoom: %3.2f, x: %d, y: %d)", Scale, OffsetX-256, OffsetY-256);
  89. AssignTexture();
  90. int TexW = GlobalTexture->GetWidth();
  91. int TexH = GlobalTexture->GetHeight();
  92. GUIRectangle SRect = Panel1->GetScreenRect();
  93. cliper.Push();
  94. cliper.SetRectangle(SRect);
  95. float ImageWidth = SRect.w * Scale;
  96. float ImageHeight = SRect.h * Scale;
  97. float ImageLeft = (float)(SRect.x+OffsetX);
  98. float ImageTop = (float)(SRect.y+OffsetY);
  99. ImageLeft -= (SRect.w * Scale * 0.5f);
  100. ImageTop -= (SRect.w * Scale * 0.5f);
  101. GUIHelper::DrawSprite((int)ImageLeft, (int)ImageTop, (int)ImageWidth, (int)ImageHeight, GlobalTexture);
  102. float XCount = TexW / GridMode;
  103. float YCount = TexH / GridMode;
  104. float ScreenStepX = (int)ImageWidth / XCount;
  105. float ScreenStepY = (int)ImageHeight / YCount;
  106. for (int x = 0; x <= XCount; x++)
  107. {
  108. float offset = ScreenStepX*x;
  109. GUIHelper::Draw2DLine(ImageLeft+offset, ImageTop, ImageLeft+offset, ImageTop+ImageHeight, GridLineColor);
  110. }
  111. for (int y = 0; y <= YCount; y++)
  112. {
  113. float offset = ScreenStepY*y;
  114. GUIHelper::Draw2DLine(ImageLeft, ImageTop+offset, ImageLeft+ImageWidth, ImageTop+offset, GridLineColor);
  115. }
  116. if (index >= 0)
  117. {
  118. XCount = TexW / Frames[index].selectionW;
  119. YCount = TexH / Frames[index].selectionH;
  120. ScreenStepX = (int)ImageWidth / XCount;
  121. ScreenStepY = (int)ImageHeight / YCount;
  122. float offsetX = ScreenStepX*Frames[index].selectionX;
  123. float offsetY = ScreenStepY*Frames[index].selectionY;
  124. GUIHelper::Draw2DLine(ImageLeft+offsetX, ImageTop+offsetY, ImageLeft+offsetX+ScreenStepX+1, ImageTop+offsetY, SelectionColor);
  125. GUIHelper::Draw2DLine(ImageLeft+offsetX, ImageTop+offsetY+ScreenStepY, ImageLeft+offsetX+ScreenStepX, ImageTop+offsetY+ScreenStepY, SelectionColor);
  126. GUIHelper::Draw2DLine(ImageLeft+offsetX, ImageTop+offsetY, ImageLeft+offsetX, ImageTop+offsetY+ScreenStepY, SelectionColor);
  127. GUIHelper::Draw2DLine(ImageLeft+offsetX+ScreenStepX, ImageTop+offsetY, ImageLeft+offsetX+ScreenStepX, ImageTop+offsetY+ScreenStepY, SelectionColor);
  128. }
  129. cliper.Pop();
  130. }
  131. void _cdecl TTextureSelector::btn16Pressed (GUIControl* sender)
  132. {
  133. GridMode = 16.0f;
  134. }
  135. void _cdecl TTextureSelector::btn32Pressed (GUIControl* sender)
  136. {
  137. GridMode = 32.0f;
  138. }
  139. void _cdecl TTextureSelector::btn64Pressed (GUIControl* sender)
  140. {
  141. GridMode = 64.0f;
  142. }
  143. void _cdecl TTextureSelector::btn128Pressed (GUIControl* sender)
  144. {
  145. GridMode = 128.0f;
  146. }
  147. bool TTextureSelector::ProcessMessages(GUIMessage message, DWORD lparam, DWORD hparam)
  148. {
  149. if (message == GUIMSG_WHEEL_DOWN)
  150. {
  151. Scale += 0.1f;
  152. if (Scale > 10.0f) Scale = 10.0f;
  153. }
  154. if (message == GUIMSG_WHEEL_UP)
  155. {
  156. Scale -= 0.1f;
  157. if (Scale < 1.0f) Scale = 1.0f;
  158. }
  159. return GUIWindow::ProcessMessages(message, lparam, hparam);
  160. }
  161. void TTextureSelector::MouseMove (int button, const GUIPoint& pt)
  162. {
  163. //if (api->Controls->GetControlStateType("RightMouseButton") == CST_ACTIVE)
  164. if (Panel1->RMB_Pressed)
  165. {
  166. OffsetX += (int)Application->GetCursor()->DeltaX;
  167. OffsetY += (int)Application->GetCursor()->DeltaY;
  168. }
  169. }
  170. void _cdecl TTextureSelector::RenderGridColor (GUIControl* sender)
  171. {
  172. GUIRectangle SRect = GridColor->GetScreenRect();
  173. GUIHelper::Draw2DRect(SRect.x, SRect.y, SRect.w, SRect.h, GridLineColor);
  174. }
  175. void _cdecl TTextureSelector::SelectColorDialog (GUIControl* sender)
  176. {
  177. GUIColorPicker* cPicker = NEW GUIColorPicker (300, 100, "Select grid color", 0, 0);
  178. Color value(GridLineColor);
  179. cPicker->editA->Text = IntToStr ((int)(value.a * 255.0f));
  180. cPicker->editR->Text = IntToStr ((int)(value.r * 255.0f));
  181. cPicker->editG->Text = IntToStr ((int)(value.g * 255.0f));
  182. cPicker->editB->Text = IntToStr ((int)(value.b * 255.0f));
  183. cPicker->TextIsChanged (NULL);
  184. cPicker->OnApply = (CONTROL_EVENT)&TTextureSelector::ApplyColor;
  185. Application->ShowModal(cPicker);
  186. //ColorEdit->SelectedColor
  187. }
  188. void _cdecl TTextureSelector::ApplyColor (GUIControl* sender)
  189. {
  190. GUIColorPicker* cPicker = (GUIColorPicker*)sender;
  191. GridLineColor = cPicker->SelectedColor;
  192. }
  193. void _cdecl TTextureSelector::btnAddFramePressed (GUIControl* sender)
  194. {
  195. FrameInfo Empty;
  196. Empty.U = 0.0f;
  197. Empty.V = 0.0f;
  198. Empty.H = 0.0f;
  199. Empty.W = 0.0f;
  200. Empty.Draw = false;
  201. Frames.Add(Empty);
  202. string StrFrame;
  203. StrFrame.Format("Frame %d", (Frames.Size()-1));
  204. lstFrames->Items.Add(StrFrame);
  205. }
  206. void _cdecl TTextureSelector::btnRemoveFramePressed (GUIControl* sender)
  207. {
  208. int index = lstFrames->SelectedLine;
  209. if (index == -1) return;
  210. Frames.Extract(index);
  211. lstFrames->Items.Delete(index);
  212. lstFrames->SelectedLine = -1;
  213. }
  214. void _cdecl TTextureSelector::SelectFrame (GUIControl* sender)
  215. {
  216. int index = lstFrames->SelectedLine;
  217. if (index == -1) return;
  218. int TexW = GlobalTexture->GetWidth();
  219. int TexH = GlobalTexture->GetHeight();
  220. GUIRectangle SRect = Panel1->GetScreenRect();
  221. float ImageWidth = SRect.w * Scale;
  222. float ImageHeight = SRect.h * Scale;
  223. float ImageLeft = (float)(SRect.x+OffsetX);
  224. float ImageTop = (float)(SRect.y+OffsetY);
  225. ImageLeft -= (SRect.w * Scale * 0.5f);
  226. ImageTop -= (SRect.w * Scale * 0.5f);
  227. float XCount = TexW / GridMode;
  228. float YCount = TexH / GridMode;
  229. float GridSizeFloat = GridMode;
  230. float ScreenStepX = (int)ImageWidth / XCount;
  231. float ScreenStepY = (int)ImageHeight / YCount;
  232. int XSelected = -1;
  233. int YSelected = -1;
  234. float MouseX;
  235. float MouseY;
  236. GUIPoint mpos = Application->GetCursor()->GetPosition();
  237. MouseX = (float)mpos.x;
  238. MouseY = (float)mpos.y;
  239. for (int x = 0; x <= XCount; x++)
  240. {
  241. float offset = ScreenStepX*x;
  242. if (MouseX >= (ImageLeft+offset) && MouseX <= (ImageLeft+offset+ScreenStepX))
  243. {
  244. XSelected = x;
  245. break;
  246. }
  247. //GUIHelper::Draw2DLine(ImageLeft+offset, ImageTop, ImageLeft+offset, ImageTop+ImageHeight, GridLineColor);
  248. }
  249. for (int y = 0; y <= YCount; y++)
  250. {
  251. float offset = ScreenStepY*y;
  252. if (MouseY >= (ImageTop+offset) && MouseY <= (ImageTop+offset+ScreenStepY))
  253. {
  254. YSelected = y;
  255. break;
  256. }
  257. //GUIHelper::Draw2DLine(ImageLeft, ImageTop+offset, ImageLeft+ImageWidth, ImageTop+offset, GridLineColor);
  258. }
  259. if (XSelected == -1) return;
  260. if (YSelected == -1) return;
  261. float TexelX = XSelected * GridSizeFloat;
  262. float TexelY = YSelected * GridSizeFloat;
  263. float TexelWidth = GridSizeFloat;
  264. float TexelHeight = GridSizeFloat;
  265. TexelX = TexelX / TexW;
  266. TexelY = TexelY / TexH;
  267. TexelWidth = TexelWidth / TexW;
  268. TexelHeight = TexelHeight / TexH;
  269. Frames[index].U = TexelX;
  270. Frames[index].V = TexelY;
  271. Frames[index].W = TexelWidth;
  272. Frames[index].H = TexelHeight;
  273. Frames[index].Draw = true;
  274. Frames[index].selectionX = (float)XSelected;
  275. Frames[index].selectionY = (float)YSelected;
  276. Frames[index].selectionH = GridSizeFloat;
  277. Frames[index].selectionW = GridSizeFloat;
  278. }
  279. void _cdecl TTextureSelector::RenderSelColor (GUIControl* sender)
  280. {
  281. GUIRectangle SRect = SelectColor->GetScreenRect();
  282. GUIHelper::Draw2DRect(SRect.x, SRect.y, SRect.w, SRect.h, SelectionColor);
  283. }
  284. void _cdecl TTextureSelector::SelectColorDialog2 (GUIControl* sender)
  285. {
  286. GUIColorPicker* cPicker = NEW GUIColorPicker (300, 100, "Select Selection color", 0, 0);
  287. Color value(SelectionColor);
  288. cPicker->editA->Text = IntToStr ((int)(value.a * 255.0f));
  289. cPicker->editR->Text = IntToStr ((int)(value.r * 255.0f));
  290. cPicker->editG->Text = IntToStr ((int)(value.g * 255.0f));
  291. cPicker->editB->Text = IntToStr ((int)(value.b * 255.0f));
  292. cPicker->TextIsChanged (NULL);
  293. cPicker->OnApply = (CONTROL_EVENT)&TTextureSelector::ApplyColor2;
  294. Application->ShowModal(cPicker);
  295. }
  296. void _cdecl TTextureSelector::ApplyColor2 (GUIControl* sender)
  297. {
  298. GUIColorPicker* cPicker = (GUIColorPicker*)sender;
  299. SelectionColor = cPicker->SelectedColor;
  300. }
  301. void TTextureSelector::AddFrame (float U, float V, float W, float H)
  302. {
  303. AssignTexture();
  304. int TexW = GlobalTexture->GetWidth();
  305. int TexH = GlobalTexture->GetHeight();
  306. FrameInfo Empty;
  307. //Переводим в пиксели...
  308. Empty.selectionW = W * TexW;
  309. Empty.selectionH = W * TexH;
  310. Empty.selectionX = U * TexW;
  311. Empty.selectionY = V * TexH;
  312. //Рассчитываем позицию...
  313. Empty.selectionX /= Empty.selectionW;
  314. Empty.selectionY /= Empty.selectionH;
  315. Empty.U = U;
  316. Empty.V = V;
  317. Empty.H = H;
  318. Empty.W = W;
  319. Empty.Draw = true;
  320. Frames.Add(Empty);
  321. string StrFrame;
  322. StrFrame.Format("Frame %d", (Frames.Size()-1));
  323. lstFrames->Items.Add(StrFrame);
  324. }
  325. int TTextureSelector::GetFrameCount ()
  326. {
  327. return Frames.Size();
  328. }
  329. void TTextureSelector::GetFrame (int index, float& U, float& V, float& W, float& H)
  330. {
  331. U = Frames[index].U;
  332. V = Frames[index].V;
  333. W = Frames[index].W;
  334. H = Frames[index].H;
  335. }
  336. void TTextureSelector::AssignTexture ()
  337. {
  338. IParticleService* pPS = (IParticleService*)api->GetService("ParticleService");
  339. Assert (pPS);
  340. DWORD mCount = pPS->GetManagersCount();
  341. if (mCount <= 0) return;
  342. IParticleManager* pManager = pPS->GetManagerByIndex(0);
  343. if (GlobalTexture->IsEmpty())
  344. {
  345. if (!GlobalTexture->DirectLoadTGA("particles"))
  346. {
  347. GlobalTexture->DirectLoad("particles");
  348. }
  349. int tTexW = GlobalTexture->GetWidth();
  350. int tTexH = GlobalTexture->GetHeight();
  351. OffsetX = 256;
  352. OffsetY = 256;
  353. }
  354. }
  355. #endif