[Gcblue-commits] gcb_wx/src/graphics tcNumberWidget.cpp,NONE,1.1
Status: Alpha
Brought to you by:
ddcforge
|
From: Dewitt C. <ddc...@us...> - 2004-12-28 00:41:45
|
Update of /cvsroot/gcblue/gcb_wx/src/graphics In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv10691/src/graphics Added Files: tcNumberWidget.cpp Log Message: --- NEW FILE: tcNumberWidget.cpp --- /** ** @file tcNumberWidget.cpp */ /* Copyright (C) 2004 Dewitt Colclough (de...@tw...) ** All rights reserved. ** This file is part of the Global Conflict Blue (GCB) program. ** GCB is free software; you can redistribute it and/or modify ** it under the terms of version 2 of the GNU General Public License as ** published by the Free Software Foundation. ** GCB 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 GCB; if not, write to the Free Software ** Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ #include "stdwx.h" // precompiled header file #ifndef WX_PRECOMP #include "wx/wx.h" #endif #include "tcNumberWidget.h" #include <osg/Texture2D> #include <osg/Vec4> #include "wxcommands.h" #include "tcSound.h" #include "tcTime.h" #include <iostream> #ifdef _DEBUG #define new DEBUG_NEW #endif /** * Drawing Graphics object is based on parent's window since * button is sharing a surface with the parent. * This also allows the button to draw outside of its wxWindow * screen area. */ void tcNumberWidget::Draw() { if (!mbActive) return; UpdateAutoChange(); // non-image drawing osg::Vec4 backgroundColor; osg::Vec4 textColor; if (isMouseOver) { backgroundColor.set(0.1, 0.1, 0.1, backgroundAlpha); textColor.set(1, 1, 1, 1); } else { backgroundColor.set(0.0, 0.0, 0.0, backgroundAlpha); textColor.set(1, 1, 1, backgroundAlpha); } DrawRectangle(0, 0, mnWidth, mnHeight, backgroundColor, FILL_ON); DrawRectangle(0, 0, mnWidth, mnHeight, osg::Vec4(1, 1, 1, backgroundAlpha), FILL_OFF); float x = float(mnWidth) - 3.0f; float y = 0.5f * float(mnHeight); // draw caption to the left of the window area DrawText(caption.c_str(), -5.0f, y, defaultFont.get(), osg::Vec4(1, 1, 1, backgroundAlpha), fontSize, RIGHT_CENTER); wxString s = wxString::Format("%.0f", val); DrawText(s.c_str(), x, y, defaultFont.get(), textColor, fontSize, RIGHT_CENTER); HideUnusedObjects(); } /** * */ void tcNumberWidget::OnEnterWindow(wxMouseEvent& event) { if (!isMouseOver) { tcSound::Get()->PlayEffect(soundEffect); } isMouseOver = true; if (sendRedraw) { SendRedraw(); } } /** * */ void tcNumberWidget::OnLButtonDown(wxMouseEvent& event) { wxPoint pos = event.GetPosition(); leftButtonDown = true; buttonDownTime = tcTime::Get()->Get30HzCount(); bool upper = pos.y < mnHeight / 2; // measure could be done once per font size change instead wxSize numberSize; MeasureText(defaultFont.get(), fontSize, "8", numberSize); float digitIdx = floorf(float(mnWidth - pos.x - 2.0f) / float(numberSize.GetWidth())); if (digitIdx < 0) digitIdx = 0; else if (digitIdx > 3) digitIdx = 3; float increment = powf(10.0f, digitIdx); if (upper) { changeIncrement = increment; } else { changeIncrement = -increment; } SendRedraw(); tcSound::Get()->PlayEffect(19); } /** * */ void tcNumberWidget::OnLButtonUp(wxMouseEvent& event) { // implement change for simple click on release of mouse if (!autoChange) { val += changeIncrement; } autoChange = false; changeIncrement = 0; leftButtonDown = false; SendRedraw(); } /** * */ void tcNumberWidget::OnLeaveWindow(wxMouseEvent& event) { isMouseOver = false; leftButtonDown = false; if (sendRedraw) { SendRedraw(); } } /** * */ void tcNumberWidget::OnRButtonDown(wxMouseEvent& event) { event.Skip(); } /** * Send ID_BUTTONREDRAW message to force parent redraw */ void tcNumberWidget::SendRedraw() { wxCommandEvent cmd(wxEVT_COMMAND_BUTTON_CLICKED, ID_BUTTONREDRAW) ; cmd.SetEventObject(this); AddPendingEvent(cmd); } /** * */ void tcNumberWidget::SetBackgroundAlpha(float val) { backgroundAlpha = val; } /** * */ void tcNumberWidget::SetFontSize(float size) { fontSize = size; } /** * */ void tcNumberWidget::SetSendRedraw(bool state) { sendRedraw = state; } /** * Check if mouse button down long enough and * periodically increment / decrement val if so * Allows user to change value quickly by holding mouse * button down. */ void tcNumberWidget::UpdateAutoChange() { unsigned currentTime = tcTime::Get()->Get30HzCount(); if (currentTime - buttonDownTime > 20) { autoChange = true; } if (autoChange) { val += changeIncrement; } if (val < 0) val = 0; } /** * */ tcNumberWidget::tcNumberWidget(float& param, tc3DWindow *parent, const wxPoint& pos, const wxSize& size, const wxString& name) : tc3DWindow(parent, pos, size, name, parent), val(param), sendRedraw(false), autoChange(false), changeIncrement(0), leftButtonDown(false), buttonDownTime(0) { caption = "NULL"; isMouseOver = false; soundEffect = -1; fontSize = 10.0f; backgroundAlpha = 1.0f; } tcNumberWidget::~tcNumberWidget() { #ifdef _DEBUG fprintf(stdout, "Destroying number widget\n"); #endif } |