Commit c7713661 authored by Fluorohydride's avatar Fluorohydride

update

parent a7da0e4c
// Copyright (C) 2002-2012 Nikolaus Gebhardt
// This file is part of the "Irrlicht Engine".
// For conditions of distribution and use, see copyright notice in irrlicht.h
#include "CGUIButton.h"
#ifdef _IRR_COMPILE_WITH_GUI_
#include "IGUISkin.h"
#include "IGUIEnvironment.h"
#include "IVideoDriver.h"
#include "IGUIFont.h"
#include "os.h"
namespace irr
{
namespace gui
{
//! constructor
CGUIButton::CGUIButton(IGUIEnvironment* environment, IGUIElement* parent,
s32 id, core::rect<s32> rectangle, bool noclip)
: IGUIButton(environment, parent, id, rectangle),
SpriteBank(0), OverrideFont(0), Image(0), PressedImage(0),
ClickTime(0), HoverTime(0), FocusTime(0),
IsPushButton(false), Pressed(false),
UseAlphaChannel(false), DrawBorder(true), ScaleImage(false)
{
#ifdef _DEBUG
setDebugName("CGUIButton");
#endif
setNotClipped(noclip);
// Initialize the sprites.
for (u32 i=0; i<EGBS_COUNT; ++i)
ButtonSprites[i].Index = -1;
// This element can be tabbed.
setTabStop(true);
setTabOrder(-1);
}
//! destructor
CGUIButton::~CGUIButton()
{
if (OverrideFont)
OverrideFont->drop();
if (Image)
Image->drop();
if (PressedImage)
PressedImage->drop();
if (SpriteBank)
SpriteBank->drop();
}
//! Sets if the images should be scaled to fit the button
void CGUIButton::setScaleImage(bool scaleImage)
{
ScaleImage = scaleImage;
}
//! Returns whether the button scale the used images
bool CGUIButton::isScalingImage() const
{
_IRR_IMPLEMENT_MANAGED_MARSHALLING_BUGFIX;
return ScaleImage;
}
//! Sets if the button should use the skin to draw its border
void CGUIButton::setDrawBorder(bool border)
{
DrawBorder = border;
}
void CGUIButton::setSpriteBank(IGUISpriteBank* sprites)
{
if (sprites)
sprites->grab();
if (SpriteBank)
SpriteBank->drop();
SpriteBank = sprites;
}
void CGUIButton::setSprite(EGUI_BUTTON_STATE state, s32 index, video::SColor color, bool loop)
{
if (SpriteBank)
{
ButtonSprites[(u32)state].Index = index;
ButtonSprites[(u32)state].Color = color;
ButtonSprites[(u32)state].Loop = loop;
}
else
{
ButtonSprites[(u32)state].Index = -1;
}
}
//! called if an event happened.
bool CGUIButton::OnEvent(const SEvent& event)
{
if (!isEnabled())
return IGUIElement::OnEvent(event);
switch(event.EventType)
{
case EET_KEY_INPUT_EVENT:
if (event.KeyInput.PressedDown &&
(event.KeyInput.Key == KEY_RETURN || event.KeyInput.Key == KEY_SPACE))
{
if (!IsPushButton)
setPressed(true);
else
setPressed(!Pressed);
return true;
}
if (Pressed && !IsPushButton && event.KeyInput.PressedDown && event.KeyInput.Key == KEY_ESCAPE)
{
setPressed(false);
return true;
}
else
if (!event.KeyInput.PressedDown && Pressed &&
(event.KeyInput.Key == KEY_RETURN || event.KeyInput.Key == KEY_SPACE))
{
if (!IsPushButton)
setPressed(false);
if (Parent)
{
SEvent newEvent;
newEvent.EventType = EET_GUI_EVENT;
newEvent.GUIEvent.Caller = this;
newEvent.GUIEvent.Element = 0;
newEvent.GUIEvent.EventType = EGET_BUTTON_CLICKED;
Parent->OnEvent(newEvent);
}
return true;
}
break;
case EET_GUI_EVENT:
if (event.GUIEvent.Caller == this)
{
if (event.GUIEvent.EventType == EGET_ELEMENT_FOCUS_LOST)
{
if (!IsPushButton)
setPressed(false);
FocusTime = os::Timer::getTime();
}
else if (event.GUIEvent.EventType == EGET_ELEMENT_FOCUSED)
{
FocusTime = os::Timer::getTime();
}
else if (event.GUIEvent.EventType == EGET_ELEMENT_HOVERED || event.GUIEvent.EventType == EGET_ELEMENT_LEFT)
{
HoverTime = os::Timer::getTime();
}
}
break;
case EET_MOUSE_INPUT_EVENT:
if (event.MouseInput.Event == EMIE_LMOUSE_PRESSED_DOWN)
{
if (Environment->hasFocus(this) &&
!AbsoluteClippingRect.isPointInside(core::position2d<s32>(event.MouseInput.X, event.MouseInput.Y)))
{
Environment->removeFocus(this);
return false;
}
if (!IsPushButton)
setPressed(true);
Environment->setFocus(this);
return true;
}
else
if (event.MouseInput.Event == EMIE_LMOUSE_LEFT_UP)
{
bool wasPressed = Pressed;
if ( !AbsoluteClippingRect.isPointInside( core::position2d<s32>(event.MouseInput.X, event.MouseInput.Y ) ) )
{
if (!IsPushButton)
setPressed(false);
return true;
}
if (!IsPushButton)
setPressed(false);
else
{
setPressed(!Pressed);
}
if ((!IsPushButton && wasPressed && Parent) ||
(IsPushButton && wasPressed != Pressed))
{
SEvent newEvent;
newEvent.EventType = EET_GUI_EVENT;
newEvent.GUIEvent.Caller = this;
newEvent.GUIEvent.Element = 0;
newEvent.GUIEvent.EventType = EGET_BUTTON_CLICKED;
Parent->OnEvent(newEvent);
}
return true;
}
break;
default:
break;
}
return Parent ? Parent->OnEvent(event) : false;
}
//! draws the element and its children
void CGUIButton::draw()
{
if (!IsVisible)
return;
IGUISkin* skin = Environment->getSkin();
video::IVideoDriver* driver = Environment->getVideoDriver();
// todo: move sprite up and text down if the pressed state has a sprite
const core::position2di spritePos = AbsoluteRect.getCenter();
if (!Pressed)
{
if (DrawBorder)
skin->draw3DButtonPaneStandard(this, AbsoluteRect, &AbsoluteClippingRect);
if (Image)
{
core::position2d<s32> pos = spritePos;
pos.X -= ImageRect.getWidth() / 2;
pos.Y -= ImageRect.getHeight() / 2;
driver->draw2DImage(Image,
ScaleImage? AbsoluteRect :
core::recti(pos, ImageRect.getSize()),
ImageRect, &AbsoluteClippingRect,
0, UseAlphaChannel);
}
}
else
{
if (DrawBorder)
skin->draw3DButtonPanePressed(this, AbsoluteRect, &AbsoluteClippingRect);
if (PressedImage)
{
core::position2d<s32> pos = spritePos;
pos.X -= PressedImageRect.getWidth() / 2;
pos.Y -= PressedImageRect.getHeight() / 2;
if (Image == PressedImage && PressedImageRect == ImageRect)
{
pos.X += skin->getSize(EGDS_BUTTON_PRESSED_IMAGE_OFFSET_X);
pos.Y += skin->getSize(EGDS_BUTTON_PRESSED_IMAGE_OFFSET_Y);
}
driver->draw2DImage(PressedImage,
ScaleImage? AbsoluteRect :
core::recti(pos, PressedImageRect.getSize()),
PressedImageRect, &AbsoluteClippingRect,
0, UseAlphaChannel);
}
}
if (SpriteBank)
{
// pressed / unpressed animation
u32 state = Pressed ? (u32)EGBS_BUTTON_DOWN : (u32)EGBS_BUTTON_UP;
if (ButtonSprites[state].Index != -1)
{
SpriteBank->draw2DSprite(ButtonSprites[state].Index, spritePos,
&AbsoluteClippingRect, ButtonSprites[state].Color, ClickTime, os::Timer::getTime(),
ButtonSprites[state].Loop, true);
}
// focused / unfocused animation
state = Environment->hasFocus(this) ? (u32)EGBS_BUTTON_FOCUSED : (u32)EGBS_BUTTON_NOT_FOCUSED;
if (ButtonSprites[state].Index != -1)
{
SpriteBank->draw2DSprite(ButtonSprites[state].Index, spritePos,
&AbsoluteClippingRect, ButtonSprites[state].Color, FocusTime, os::Timer::getTime(),
ButtonSprites[state].Loop, true);
}
// mouse over / off animation
if (isEnabled())
{
state = Environment->getHovered() == this ? (u32)EGBS_BUTTON_MOUSE_OVER : (u32)EGBS_BUTTON_MOUSE_OFF;
if (ButtonSprites[state].Index != -1)
{
SpriteBank->draw2DSprite(ButtonSprites[state].Index, spritePos,
&AbsoluteClippingRect, ButtonSprites[state].Color, HoverTime, os::Timer::getTime(),
ButtonSprites[state].Loop, true);
}
}
}
if (Text.size())
{
IGUIFont* font = getActiveFont();
core::rect<s32> rect = AbsoluteRect;
if (Pressed)
{
rect.UpperLeftCorner.X += skin->getSize(EGDS_BUTTON_PRESSED_TEXT_OFFSET_X);
rect.UpperLeftCorner.Y += skin->getSize(EGDS_BUTTON_PRESSED_TEXT_OFFSET_Y);
}
if (font)
font->draw(Text.c_str(), rect,
skin->getColor(isEnabled() ? EGDC_BUTTON_TEXT : EGDC_GRAY_TEXT),
true, true, &AbsoluteClippingRect);
}
IGUIElement::draw();
}
//! sets another skin independent font. if this is set to zero, the button uses the font of the skin.
void CGUIButton::setOverrideFont(IGUIFont* font)
{
if (OverrideFont == font)
return;
if (OverrideFont)
OverrideFont->drop();
OverrideFont = font;
if (OverrideFont)
OverrideFont->grab();
}
//! Gets the override font (if any)
IGUIFont * CGUIButton::getOverrideFont() const
{
return OverrideFont;
}
//! Get the font which is used right now for drawing
IGUIFont* CGUIButton::getActiveFont() const
{
if ( OverrideFont )
return OverrideFont;
IGUISkin* skin = Environment->getSkin();
if (skin)
return skin->getFont(EGDF_BUTTON);
return 0;
}
//! Sets an image which should be displayed on the button when it is in normal state.
void CGUIButton::setImage(video::ITexture* image)
{
if (image)
image->grab();
if (Image)
Image->drop();
Image = image;
if (image)
ImageRect = core::rect<s32>(core::position2d<s32>(0,0), image->getOriginalSize());
if (!PressedImage)
setPressedImage(Image);
}
//! Sets the image which should be displayed on the button when it is in its normal state.
void CGUIButton::setImage(video::ITexture* image, const core::rect<s32>& pos)
{
setImage(image);
ImageRect = pos;
}
//! Sets an image which should be displayed on the button when it is in pressed state.
void CGUIButton::setPressedImage(video::ITexture* image)
{
if (image)
image->grab();
if (PressedImage)
PressedImage->drop();
PressedImage = image;
if (image)
PressedImageRect = core::rect<s32>(core::position2d<s32>(0,0), image->getOriginalSize());
}
//! Sets the image which should be displayed on the button when it is in its pressed state.
void CGUIButton::setPressedImage(video::ITexture* image, const core::rect<s32>& pos)
{
setPressedImage(image);
PressedImageRect = pos;
}
//! Sets if the button should behave like a push button. Which means it
//! can be in two states: Normal or Pressed. With a click on the button,
//! the user can change the state of the button.
void CGUIButton::setIsPushButton(bool isPushButton)
{
IsPushButton = isPushButton;
}
//! Returns if the button is currently pressed
bool CGUIButton::isPressed() const
{
_IRR_IMPLEMENT_MANAGED_MARSHALLING_BUGFIX;
return Pressed;
}
//! Sets the pressed state of the button if this is a pushbutton
void CGUIButton::setPressed(bool pressed)
{
if (Pressed != pressed)
{
ClickTime = os::Timer::getTime();
Pressed = pressed;
}
}
//! Returns whether the button is a push button
bool CGUIButton::isPushButton() const
{
_IRR_IMPLEMENT_MANAGED_MARSHALLING_BUGFIX;
return IsPushButton;
}
//! Sets if the alpha channel should be used for drawing images on the button (default is false)
void CGUIButton::setUseAlphaChannel(bool useAlphaChannel)
{
UseAlphaChannel = useAlphaChannel;
}
//! Returns if the alpha channel should be used for drawing images on the button
bool CGUIButton::isAlphaChannelUsed() const
{
_IRR_IMPLEMENT_MANAGED_MARSHALLING_BUGFIX;
return UseAlphaChannel;
}
bool CGUIButton::isDrawingBorder() const
{
_IRR_IMPLEMENT_MANAGED_MARSHALLING_BUGFIX;
return DrawBorder;
}
//! Writes attributes of the element.
void CGUIButton::serializeAttributes(io::IAttributes* out, io::SAttributeReadWriteOptions* options=0) const
{
IGUIButton::serializeAttributes(out,options);
out->addBool ("PushButton", IsPushButton );
if (IsPushButton)
out->addBool("Pressed", Pressed);
out->addTexture ("Image", Image);
out->addRect ("ImageRect", ImageRect);
out->addTexture ("PressedImage", PressedImage);
out->addRect ("PressedImageRect", PressedImageRect);
out->addBool ("UseAlphaChannel", isAlphaChannelUsed());
out->addBool ("Border", isDrawingBorder());
out->addBool ("ScaleImage", isScalingImage());
// out->addString ("OverrideFont", OverrideFont);
}
//! Reads attributes of the element
void CGUIButton::deserializeAttributes(io::IAttributes* in, io::SAttributeReadWriteOptions* options=0)
{
IGUIButton::deserializeAttributes(in,options);
IsPushButton = in->getAttributeAsBool("PushButton");
Pressed = IsPushButton ? in->getAttributeAsBool("Pressed") : false;
core::rect<s32> rec = in->getAttributeAsRect("ImageRect");
if (rec.isValid())
setImage( in->getAttributeAsTexture("Image"), rec);
else
setImage( in->getAttributeAsTexture("Image") );
rec = in->getAttributeAsRect("PressedImageRect");
if (rec.isValid())
setPressedImage( in->getAttributeAsTexture("PressedImage"), rec);
else
setPressedImage( in->getAttributeAsTexture("PressedImage") );
setDrawBorder(in->getAttributeAsBool("Border"));
setUseAlphaChannel(in->getAttributeAsBool("UseAlphaChannel"));
setScaleImage(in->getAttributeAsBool("ScaleImage"));
// setOverrideFont(in->getAttributeAsString("OverrideFont"));
updateAbsolutePosition();
}
} // end namespace gui
} // end namespace irr
#endif // _IRR_COMPILE_WITH_GUI_
// Copyright (C) 2002-2010 Nikolaus Gebhardt
// This file is part of the "Irrlicht Engine".
// For conditions of distribution and use, see copyright notice in irrlicht.h
#include "CGUIEditBox.h"
#ifdef _IRR_COMPILE_WITH_GUI_
#include "IGUISkin.h"
#include "IGUIEnvironment.h"
#include "IGUIFont.h"
#include "IVideoDriver.h"
#include "rect.h"
#include "os.h"
#include "Keycodes.h"
/*
todo:
optional scrollbars
ctrl+left/right to select word
double click/ctrl click: word select + drag to select whole words, triple click to select line
optional? dragging selected text
numerical
*/
namespace irr
{
namespace gui
{
//! constructor
CGUIEditBox::CGUIEditBox(const wchar_t* text, bool border,
IGUIEnvironment* environment, IGUIElement* parent, s32 id,
const core::rect<s32>& rectangle)
: IGUIEditBox(environment, parent, id, rectangle), MouseMarking(false),
Border(border), OverrideColorEnabled(false), MarkBegin(0), MarkEnd(0),
OverrideColor(video::SColor(101,255,255,255)), OverrideFont(0), LastBreakFont(0),
Operator(0), BlinkStartTime(0), CursorPos(0), HScrollPos(0), VScrollPos(0), Max(0),
WordWrap(false), MultiLine(false), AutoScroll(true), PasswordBox(false),
PasswordChar(L'*'), HAlign(EGUIA_UPPERLEFT), VAlign(EGUIA_CENTER),
CurrentTextRect(0,0,1,1), FrameRect(rectangle)
{
#ifdef _DEBUG
setDebugName("CGUIEditBox");
#endif
Text = text;
if (Environment)
Operator = Environment->getOSOperator();
if (Operator)
Operator->grab();
// this element can be tabbed to
setTabStop(true);
setTabOrder(-1);
IGUISkin *skin = 0;
if (Environment)
skin = Environment->getSkin();
if (Border && skin)
{
FrameRect.UpperLeftCorner.X += skin->getSize(EGDS_TEXT_DISTANCE_X)+1;
FrameRect.UpperLeftCorner.Y += skin->getSize(EGDS_TEXT_DISTANCE_Y)+1;
FrameRect.LowerRightCorner.X -= skin->getSize(EGDS_TEXT_DISTANCE_X)+1;
FrameRect.LowerRightCorner.Y -= skin->getSize(EGDS_TEXT_DISTANCE_Y)+1;
}
breakText();
calculateScrollPos();
}
//! destructor
CGUIEditBox::~CGUIEditBox()
{
if (OverrideFont)
OverrideFont->drop();
if (Operator)
Operator->drop();
}
//! Sets another skin independent font.
void CGUIEditBox::setOverrideFont(IGUIFont* font)
{
if (OverrideFont == font)
return;
if (OverrideFont)
OverrideFont->drop();
OverrideFont = font;
if (OverrideFont)
OverrideFont->grab();
breakText();
}
//! Sets another color for the text.
void CGUIEditBox::setOverrideColor(video::SColor color)
{
OverrideColor = color;
OverrideColorEnabled = true;
}
//! Turns the border on or off
void CGUIEditBox::setDrawBorder(bool border)
{
Border = border;
}
//! Sets if the text should use the overide color or the color in the gui skin.
void CGUIEditBox::enableOverrideColor(bool enable)
{
OverrideColorEnabled = enable;
}
//! Enables or disables word wrap
void CGUIEditBox::setWordWrap(bool enable)
{
WordWrap = enable;
breakText();
}
void CGUIEditBox::updateAbsolutePosition()
{
core::rect<s32> oldAbsoluteRect(AbsoluteRect);
IGUIElement::updateAbsolutePosition();
if ( oldAbsoluteRect != AbsoluteRect )
{
breakText();
}
}
//! Checks if word wrap is enabled
bool CGUIEditBox::isWordWrapEnabled() const
{
_IRR_IMPLEMENT_MANAGED_MARSHALLING_BUGFIX;
return WordWrap;
}
//! Enables or disables newlines.
void CGUIEditBox::setMultiLine(bool enable)
{
MultiLine = enable;
}
//! Checks if multi line editing is enabled
bool CGUIEditBox::isMultiLineEnabled() const
{
_IRR_IMPLEMENT_MANAGED_MARSHALLING_BUGFIX;
return MultiLine;
}
void CGUIEditBox::setPasswordBox(bool passwordBox, wchar_t passwordChar)
{
PasswordBox = passwordBox;
if (PasswordBox)
{
PasswordChar = passwordChar;
setMultiLine(false);
setWordWrap(false);
BrokenText.clear();
}
}
bool CGUIEditBox::isPasswordBox() const
{
_IRR_IMPLEMENT_MANAGED_MARSHALLING_BUGFIX;
return PasswordBox;
}
//! Sets text justification
void CGUIEditBox::setTextAlignment(EGUI_ALIGNMENT horizontal, EGUI_ALIGNMENT vertical)
{
HAlign = horizontal;
VAlign = vertical;
}
//! called if an event happened.
bool CGUIEditBox::OnEvent(const SEvent& event)
{
if (IsEnabled)
{
switch(event.EventType)
{
case EET_GUI_EVENT:
if (event.GUIEvent.EventType == EGET_ELEMENT_FOCUS_LOST)
{
if (event.GUIEvent.Caller == this)
{
MouseMarking = false;
setTextMarkers(0,0);
}
}
break;
case EET_KEY_INPUT_EVENT:
if (processKey(event))
return true;
break;
case EET_MOUSE_INPUT_EVENT:
if (processMouse(event))
return true;
break;
default:
break;
}
}
return IGUIElement::OnEvent(event);
}
bool CGUIEditBox::processKey(const SEvent& event)
{
if (!event.KeyInput.PressedDown)
return false;
bool textChanged = false;
s32 newMarkBegin = MarkBegin;
s32 newMarkEnd = MarkEnd;
// control shortcut handling
if (event.KeyInput.Control)
{
// german backlash '\' entered with control + '?'
if ( event.KeyInput.Char == '\\' )
{
inputChar(event.KeyInput.Char);
return true;
}
switch(event.KeyInput.Key)
{
case KEY_KEY_A:
// select all
newMarkBegin = 0;
newMarkEnd = Text.size();
break;
case KEY_KEY_C:
// copy to clipboard
if (!PasswordBox && Operator && MarkBegin != MarkEnd)
{
const s32 realmbgn = MarkBegin < MarkEnd ? MarkBegin : MarkEnd;
const s32 realmend = MarkBegin < MarkEnd ? MarkEnd : MarkBegin;
#ifdef _WIN32
Operator->copyToClipboard((c8*)Text.subString(realmbgn, realmend - realmbgn).c_str());
#else
core::stringc s;
s = Text.subString(realmbgn, realmend - realmbgn).c_str();
Operator->copyToClipboard(s.c_str());
#endif
}
break;
case KEY_KEY_X:
// cut to the clipboard
if (!PasswordBox && Operator && MarkBegin != MarkEnd)
{
const s32 realmbgn = MarkBegin < MarkEnd ? MarkBegin : MarkEnd;
const s32 realmend = MarkBegin < MarkEnd ? MarkEnd : MarkBegin;
// copy
#ifdef _WIN32
Operator->copyToClipboard((c8*)Text.subString(realmbgn, realmend - realmbgn).c_str());
#else
core::stringc sc;
sc = Text.subString(realmbgn, realmend - realmbgn).c_str();
Operator->copyToClipboard(sc.c_str());
#endif
if (IsEnabled)
{
// delete
core::stringw s;
s = Text.subString(0, realmbgn);
s.append( Text.subString(realmend, Text.size()-realmend) );
Text = s;
CursorPos = realmbgn;
newMarkBegin = 0;
newMarkEnd = 0;
textChanged = true;
}
}
break;
case KEY_KEY_V:
if ( !IsEnabled )
break;
// paste from the clipboard
if (Operator)
{
const s32 realmbgn = MarkBegin < MarkEnd ? MarkBegin : MarkEnd;
const s32 realmend = MarkBegin < MarkEnd ? MarkEnd : MarkBegin;
// add new character
#ifdef _WIN32
// using unicode in windows
const wchar_t* p = (wchar_t*)Operator->getTextFromClipboard();
#else
const c8* p = Operator->getTextFromClipboard();
#endif
if (p)
{
if (MarkBegin == MarkEnd)
{
// insert text
core::stringw s = Text.subString(0, CursorPos);
s.append(p);
s.append( Text.subString(CursorPos, Text.size()-CursorPos) );
if (!Max || s.size()<=Max) // thx to Fish FH for fix
{
Text = s;
s = p;
CursorPos += s.size();
}
}
else
{
// replace text
core::stringw s = Text.subString(0, realmbgn);
s.append(p);
s.append( Text.subString(realmend, Text.size()-realmend) );
if (!Max || s.size()<=Max) // thx to Fish FH for fix
{
Text = s;
s = p;
CursorPos = realmbgn + s.size();
}
}
}
newMarkBegin = 0;
newMarkEnd = 0;
textChanged = true;
}
break;
case KEY_HOME:
// move/highlight to start of text
if (event.KeyInput.Shift)
{
newMarkEnd = CursorPos;
newMarkBegin = 0;
CursorPos = 0;
}
else
{
CursorPos = 0;
newMarkBegin = 0;
newMarkEnd = 0;
}
break;
case KEY_END:
// move/highlight to end of text
if (event.KeyInput.Shift)
{
newMarkBegin = CursorPos;
newMarkEnd = Text.size();
CursorPos = 0;
}
else
{
CursorPos = Text.size();
newMarkBegin = 0;
newMarkEnd = 0;
}
break;
default:
return false;
}
}
// default keyboard handling
else
switch(event.KeyInput.Key)
{
case KEY_END:
{
s32 p = Text.size();
if (WordWrap || MultiLine)
{
p = getLineFromPos(CursorPos);
p = BrokenTextPositions[p] + (s32)BrokenText[p].size();
if (p > 0 && (Text[p-1] == L'\r' || Text[p-1] == L'\n' ))
p-=1;
}
if (event.KeyInput.Shift)
{
if (MarkBegin == MarkEnd)
newMarkBegin = CursorPos;
newMarkEnd = p;
}
else
{
newMarkBegin = 0;
newMarkEnd = 0;
}
CursorPos = p;
BlinkStartTime = os::Timer::getTime();
}
break;
case KEY_HOME:
{
s32 p = 0;
if (WordWrap || MultiLine)
{
p = getLineFromPos(CursorPos);
p = BrokenTextPositions[p];
}
if (event.KeyInput.Shift)
{
if (MarkBegin == MarkEnd)
newMarkBegin = CursorPos;
newMarkEnd = p;
}
else
{
newMarkBegin = 0;
newMarkEnd = 0;
}
CursorPos = p;
BlinkStartTime = os::Timer::getTime();
}
break;
case KEY_RETURN:
if (MultiLine)
{
inputChar(L'\n');
return true;
}
else
{
sendGuiEvent( EGET_EDITBOX_ENTER );
}
break;
case KEY_LEFT:
if (event.KeyInput.Shift)
{
if (CursorPos > 0)
{
if (MarkBegin == MarkEnd)
newMarkBegin = CursorPos;
newMarkEnd = CursorPos-1;
}
}
else
{
newMarkBegin = 0;
newMarkEnd = 0;
}
if (CursorPos > 0) CursorPos--;
BlinkStartTime = os::Timer::getTime();
break;
case KEY_RIGHT:
if (event.KeyInput.Shift)
{
if (Text.size() > (u32)CursorPos)
{
if (MarkBegin == MarkEnd)
newMarkBegin = CursorPos;
newMarkEnd = CursorPos+1;
}
}
else
{
newMarkBegin = 0;
newMarkEnd = 0;
}
if (Text.size() > (u32)CursorPos) CursorPos++;
BlinkStartTime = os::Timer::getTime();
break;
case KEY_UP:
if (MultiLine || (WordWrap && BrokenText.size() > 1) )
{
s32 lineNo = getLineFromPos(CursorPos);
s32 mb = (MarkBegin == MarkEnd) ? CursorPos : (MarkBegin > MarkEnd ? MarkBegin : MarkEnd);
if (lineNo > 0)
{
s32 cp = CursorPos - BrokenTextPositions[lineNo];
if ((s32)BrokenText[lineNo-1].size() < cp)
CursorPos = BrokenTextPositions[lineNo-1] + (s32)BrokenText[lineNo-1].size()-1;
else
CursorPos = BrokenTextPositions[lineNo-1] + cp;
}
if (event.KeyInput.Shift)
{
newMarkBegin = mb;
newMarkEnd = CursorPos;
}
else
{
newMarkBegin = 0;
newMarkEnd = 0;
}
}
else
{
return false;
}
break;
case KEY_DOWN:
if (MultiLine || (WordWrap && BrokenText.size() > 1) )
{
s32 lineNo = getLineFromPos(CursorPos);
s32 mb = (MarkBegin == MarkEnd) ? CursorPos : (MarkBegin < MarkEnd ? MarkBegin : MarkEnd);
if (lineNo < (s32)BrokenText.size()-1)
{
s32 cp = CursorPos - BrokenTextPositions[lineNo];
if ((s32)BrokenText[lineNo+1].size() < cp)
CursorPos = BrokenTextPositions[lineNo+1] + BrokenText[lineNo+1].size()-1;
else
CursorPos = BrokenTextPositions[lineNo+1] + cp;
}
if (event.KeyInput.Shift)
{
newMarkBegin = mb;
newMarkEnd = CursorPos;
}
else
{
newMarkBegin = 0;
newMarkEnd = 0;
}
}
else
{
return false;
}
break;
case KEY_BACK:
if ( !this->IsEnabled )
break;
if (Text.size())
{
core::stringw s;
if (MarkBegin != MarkEnd)
{
// delete marked text
const s32 realmbgn = MarkBegin < MarkEnd ? MarkBegin : MarkEnd;
const s32 realmend = MarkBegin < MarkEnd ? MarkEnd : MarkBegin;
s = Text.subString(0, realmbgn);
s.append( Text.subString(realmend, Text.size()-realmend) );
Text = s;
CursorPos = realmbgn;
}
else
{
// delete text behind cursor
if (CursorPos>0)
s = Text.subString(0, CursorPos-1);
else
s = L"";
s.append( Text.subString(CursorPos, Text.size()-CursorPos) );
Text = s;
--CursorPos;
}
if (CursorPos < 0)
CursorPos = 0;
BlinkStartTime = os::Timer::getTime();
newMarkBegin = 0;
newMarkEnd = 0;
textChanged = true;
}
break;
case KEY_DELETE:
if ( !this->IsEnabled )
break;
if (Text.size() != 0)
{
core::stringw s;
if (MarkBegin != MarkEnd)
{
// delete marked text
const s32 realmbgn = MarkBegin < MarkEnd ? MarkBegin : MarkEnd;
const s32 realmend = MarkBegin < MarkEnd ? MarkEnd : MarkBegin;
s = Text.subString(0, realmbgn);
s.append( Text.subString(realmend, Text.size()-realmend) );
Text = s;
CursorPos = realmbgn;
}
else
{
// delete text before cursor
s = Text.subString(0, CursorPos);
s.append( Text.subString(CursorPos+1, Text.size()-CursorPos-1) );
Text = s;
}
if (CursorPos > (s32)Text.size())
CursorPos = (s32)Text.size();
BlinkStartTime = os::Timer::getTime();
newMarkBegin = 0;
newMarkEnd = 0;
textChanged = true;
}
break;
case KEY_ESCAPE:
case KEY_TAB:
case KEY_SHIFT:
case KEY_F1:
case KEY_F2:
case KEY_F3:
case KEY_F4:
case KEY_F5:
case KEY_F6:
case KEY_F7:
case KEY_F8:
case KEY_F9:
case KEY_F10:
case KEY_F11:
case KEY_F12:
case KEY_F13:
case KEY_F14:
case KEY_F15:
case KEY_F16:
case KEY_F17:
case KEY_F18:
case KEY_F19:
case KEY_F20:
case KEY_F21:
case KEY_F22:
case KEY_F23:
case KEY_F24:
// ignore these keys
return false;
default:
inputChar(event.KeyInput.Char);
return true;
}
// Set new text markers
setTextMarkers( newMarkBegin, newMarkEnd );
// break the text if it has changed
if (textChanged)
{
breakText();
sendGuiEvent(EGET_EDITBOX_CHANGED);
}
calculateScrollPos();
return true;
}
//! draws the element and its children
void CGUIEditBox::draw()
{
if (!IsVisible)
return;
const bool focus = Environment->hasFocus(this);
IGUISkin* skin = Environment->getSkin();
if (!skin)
return;
FrameRect = AbsoluteRect;
// draw the border
if (Border)
{
skin->draw3DSunkenPane(this, skin->getColor(EGDC_WINDOW),
false, true, FrameRect, &AbsoluteClippingRect);
FrameRect.UpperLeftCorner.X += skin->getSize(EGDS_TEXT_DISTANCE_X)+1;
FrameRect.UpperLeftCorner.Y += skin->getSize(EGDS_TEXT_DISTANCE_Y)+1;
FrameRect.LowerRightCorner.X -= skin->getSize(EGDS_TEXT_DISTANCE_X)+1;
FrameRect.LowerRightCorner.Y -= skin->getSize(EGDS_TEXT_DISTANCE_Y)+1;
}
core::rect<s32> localClipRect = FrameRect;
localClipRect.clipAgainst(AbsoluteClippingRect);
// draw the text
IGUIFont* font = OverrideFont;
if (!OverrideFont)
font = skin->getFont();
s32 cursorLine = 0;
s32 charcursorpos = 0;
if (font)
{
if (LastBreakFont != font)
{
breakText();
}
// calculate cursor pos
core::stringw *txtLine = &Text;
s32 startPos = 0;
core::stringw s, s2;
// get mark position
const bool ml = (!PasswordBox && (WordWrap || MultiLine));
const s32 realmbgn = MarkBegin < MarkEnd ? MarkBegin : MarkEnd;
const s32 realmend = MarkBegin < MarkEnd ? MarkEnd : MarkBegin;
const s32 hlineStart = ml ? getLineFromPos(realmbgn) : 0;
const s32 hlineCount = ml ? getLineFromPos(realmend) - hlineStart + 1 : 1;
const s32 lineCount = ml ? BrokenText.size() : 1;
// Save the override color information.
// Then, alter it if the edit box is disabled.
const bool prevOver = OverrideColorEnabled;
const video::SColor prevColor = OverrideColor;
if (Text.size())
{
if (!IsEnabled && !OverrideColorEnabled)
{
OverrideColorEnabled = true;
OverrideColor = skin->getColor(EGDC_GRAY_TEXT);
}
for (s32 i=0; i < lineCount; ++i)
{
setTextRect(i);
// clipping test - don't draw anything outside the visible area
core::rect<s32> c = localClipRect;
c.clipAgainst(CurrentTextRect);
if (!c.isValid())
continue;
// get current line
if (PasswordBox)
{
if (BrokenText.size() != 1)
{
BrokenText.clear();
BrokenText.push_back(core::stringw());
}
if (BrokenText[0].size() != Text.size())
{
BrokenText[0] = Text;
for (u32 q = 0; q < Text.size(); ++q)
{
BrokenText[0] [q] = PasswordChar;
}
}
txtLine = &BrokenText[0];
startPos = 0;
}
else
{
txtLine = ml ? &BrokenText[i] : &Text;
startPos = ml ? BrokenTextPositions[i] : 0;
}
// draw normal text
font->draw(txtLine->c_str(), CurrentTextRect,
OverrideColorEnabled ? OverrideColor : skin->getColor(EGDC_BUTTON_TEXT),
false, true, &localClipRect);
// draw mark and marked text
if (focus && MarkBegin != MarkEnd && i >= hlineStart && i < hlineStart + hlineCount)
{
s32 mbegin = 0, mend = 0;
s32 lineStartPos = 0, lineEndPos = txtLine->size();
if (i == hlineStart)
{
// highlight start is on this line
s = txtLine->subString(0, realmbgn - startPos);
mbegin = font->getDimension(s.c_str()).Width;
// deal with kerning
mbegin += font->getKerningWidth(
&((*txtLine)[realmbgn - startPos]),
realmbgn - startPos > 0 ? &((*txtLine)[realmbgn - startPos - 1]) : 0);
lineStartPos = realmbgn - startPos;
}
if (i == hlineStart + hlineCount - 1)
{
// highlight end is on this line
s2 = txtLine->subString(0, realmend - startPos);
mend = font->getDimension(s2.c_str()).Width;
lineEndPos = (s32)s2.size();
}
else
mend = font->getDimension(txtLine->c_str()).Width;
CurrentTextRect.UpperLeftCorner.X += mbegin;
CurrentTextRect.LowerRightCorner.X = CurrentTextRect.UpperLeftCorner.X + mend - mbegin;
// draw mark
skin->draw2DRectangle(this, skin->getColor(EGDC_HIGH_LIGHT), CurrentTextRect, &localClipRect);
// draw marked text
s = txtLine->subString(lineStartPos, lineEndPos - lineStartPos);
if (s.size())
font->draw(s.c_str(), CurrentTextRect,
OverrideColorEnabled ? OverrideColor : skin->getColor(EGDC_HIGH_LIGHT_TEXT),
false, true, &localClipRect);
}
}
// Return the override color information to its previous settings.
OverrideColorEnabled = prevOver;
OverrideColor = prevColor;
}
// draw cursor
if (WordWrap || MultiLine)
{
cursorLine = getLineFromPos(CursorPos);
txtLine = &BrokenText[cursorLine];
startPos = BrokenTextPositions[cursorLine];
}
s = txtLine->subString(0,CursorPos-startPos);
charcursorpos = font->getDimension(s.c_str()).Width +
font->getKerningWidth(L"_", CursorPos-startPos > 0 ? &((*txtLine)[CursorPos-startPos-1]) : 0);
if (focus && (os::Timer::getTime() - BlinkStartTime) % 700 < 350)
{
setTextRect(cursorLine);
CurrentTextRect.UpperLeftCorner.X += charcursorpos;
font->draw(L"_", CurrentTextRect,
OverrideColorEnabled ? OverrideColor : skin->getColor(EGDC_BUTTON_TEXT),
false, true, &localClipRect);
}
}
// draw children
IGUIElement::draw();
}
//! Sets the new caption of this element.
void CGUIEditBox::setText(const wchar_t* text)
{
Text = text;
if (u32(CursorPos) > Text.size())
CursorPos = Text.size();
HScrollPos = 0;
breakText();
}
//! Enables or disables automatic scrolling with cursor position
//! \param enable: If set to true, the text will move around with the cursor position
void CGUIEditBox::setAutoScroll(bool enable)
{
AutoScroll = enable;
}
//! Checks to see if automatic scrolling is enabled
//! \return true if automatic scrolling is enabled, false if not
bool CGUIEditBox::isAutoScrollEnabled() const
{
_IRR_IMPLEMENT_MANAGED_MARSHALLING_BUGFIX;
return AutoScroll;
}
//! Gets the area of the text in the edit box
//! \return Returns the size in pixels of the text
core::dimension2du CGUIEditBox::getTextDimension()
{
core::rect<s32> ret;
setTextRect(0);
ret = CurrentTextRect;
for (u32 i=1; i < BrokenText.size(); ++i)
{
setTextRect(i);
ret.addInternalPoint(CurrentTextRect.UpperLeftCorner);
ret.addInternalPoint(CurrentTextRect.LowerRightCorner);
}
return core::dimension2du(ret.getSize());
}
//! Sets the maximum amount of characters which may be entered in the box.
//! \param max: Maximum amount of characters. If 0, the character amount is
//! infinity.
void CGUIEditBox::setMax(u32 max)
{
Max = max;
if (Text.size() > Max && Max != 0)
Text = Text.subString(0, Max);
}
//! Returns maximum amount of characters, previously set by setMax();
u32 CGUIEditBox::getMax() const
{
return Max;
}
bool CGUIEditBox::processMouse(const SEvent& event)
{
switch(event.MouseInput.Event)
{
case irr::EMIE_LMOUSE_LEFT_UP:
if (Environment->hasFocus(this))
{
CursorPos = getCursorPos(event.MouseInput.X, event.MouseInput.Y);
if (MouseMarking)
{
setTextMarkers( MarkBegin, CursorPos );
}
MouseMarking = false;
calculateScrollPos();
return true;
}
break;
case irr::EMIE_MOUSE_MOVED:
{
if (MouseMarking)
{
CursorPos = getCursorPos(event.MouseInput.X, event.MouseInput.Y);
setTextMarkers( MarkBegin, CursorPos );
calculateScrollPos();
return true;
}
}
break;
case EMIE_LMOUSE_PRESSED_DOWN:
if (!Environment->hasFocus(this))
{
BlinkStartTime = os::Timer::getTime();
MouseMarking = true;
CursorPos = getCursorPos(event.MouseInput.X, event.MouseInput.Y);
setTextMarkers(CursorPos, CursorPos );
calculateScrollPos();
return true;
}
else
{
if (!AbsoluteClippingRect.isPointInside(
core::position2d<s32>(event.MouseInput.X, event.MouseInput.Y)))
{
return false;
}
else
{
// move cursor
CursorPos = getCursorPos(event.MouseInput.X, event.MouseInput.Y);
s32 newMarkBegin = MarkBegin;
if (!MouseMarking)
newMarkBegin = CursorPos;
MouseMarking = true;
setTextMarkers( newMarkBegin, CursorPos);
calculateScrollPos();
return true;
}
}
default:
break;
}
return false;
}
s32 CGUIEditBox::getCursorPos(s32 x, s32 y)
{
IGUIFont* font = OverrideFont;
IGUISkin* skin = Environment->getSkin();
if (!OverrideFont)
font = skin->getFont();
const u32 lineCount = (WordWrap || MultiLine) ? BrokenText.size() : 1;
core::stringw *txtLine=0;
s32 startPos=0;
x+=3;
for (u32 i=0; i < lineCount; ++i)
{
setTextRect(i);
if (i == 0 && y < CurrentTextRect.UpperLeftCorner.Y)
y = CurrentTextRect.UpperLeftCorner.Y;
if (i == lineCount - 1 && y > CurrentTextRect.LowerRightCorner.Y )
y = CurrentTextRect.LowerRightCorner.Y;
// is it inside this region?
if (y >= CurrentTextRect.UpperLeftCorner.Y && y <= CurrentTextRect.LowerRightCorner.Y)
{
// we've found the clicked line
txtLine = (WordWrap || MultiLine) ? &BrokenText[i] : &Text;
startPos = (WordWrap || MultiLine) ? BrokenTextPositions[i] : 0;
break;
}
}
if (x < CurrentTextRect.UpperLeftCorner.X)
x = CurrentTextRect.UpperLeftCorner.X;
s32 idx = font->getCharacterFromPos(Text.c_str(), x - CurrentTextRect.UpperLeftCorner.X);
// click was on or left of the line
if (idx != -1)
return idx + startPos;
// click was off the right edge of the line, go to end.
return txtLine->size() + startPos;
}
//! Breaks the single text line.
void CGUIEditBox::breakText()
{
IGUISkin* skin = Environment->getSkin();
if ((!WordWrap && !MultiLine) || !skin)
return;
BrokenText.clear(); // need to reallocate :/
BrokenTextPositions.set_used(0);
IGUIFont* font = OverrideFont;
if (!OverrideFont)
font = skin->getFont();
if (!font)
return;
LastBreakFont = font;
core::stringw line;
core::stringw word;
core::stringw whitespace;
s32 lastLineStart = 0;
s32 size = Text.size();
s32 length = 0;
s32 elWidth = RelativeRect.getWidth() - 6;
wchar_t c;
for (s32 i=0; i<size; ++i)
{
c = Text[i];
bool lineBreak = false;
if (c == L'\r') // Mac or Windows breaks
{
lineBreak = true;
c = ' ';
if (Text[i+1] == L'\n') // Windows breaks
{
Text.erase(i+1);
--size;
}
}
else if (c == L'\n') // Unix breaks
{
lineBreak = true;
c = ' ';
}
// don't break if we're not a multi-line edit box
if (!MultiLine)
lineBreak = false;
if (c == L' ' || c == 0 || i == (size-1))
{
if (word.size())
{
// here comes the next whitespace, look if
// we can break the last word to the next line.
s32 whitelgth = font->getDimension(whitespace.c_str()).Width;
s32 worldlgth = font->getDimension(word.c_str()).Width;
if (WordWrap && length + worldlgth + whitelgth > elWidth)
{
// break to next line
length = worldlgth;
BrokenText.push_back(line);
BrokenTextPositions.push_back(lastLineStart);
lastLineStart = i - (s32)word.size();
line = word;
}
else
{
// add word to line
line += whitespace;
line += word;
length += whitelgth + worldlgth;
}
word = L"";
whitespace = L"";
}
whitespace += c;
// compute line break
if (lineBreak)
{
line += whitespace;
line += word;
BrokenText.push_back(line);
BrokenTextPositions.push_back(lastLineStart);
lastLineStart = i+1;
line = L"";
word = L"";
whitespace = L"";
length = 0;
}
}
else
{
// yippee this is a word..
word += c;
}
}
line += whitespace;
line += word;
BrokenText.push_back(line);
BrokenTextPositions.push_back(lastLineStart);
}
void CGUIEditBox::setTextRect(s32 line)
{
core::dimension2du d;
IGUISkin* skin = Environment->getSkin();
if (!skin)
return;
IGUIFont* font = OverrideFont ? OverrideFont : skin->getFont();
if (!font)
return;
// get text dimension
const u32 lineCount = (WordWrap || MultiLine) ? BrokenText.size() : 1;
if (WordWrap || MultiLine)
{
d = font->getDimension(BrokenText[line].c_str());
}
else
{
d = font->getDimension(Text.c_str());
d.Height = AbsoluteRect.getHeight();
}
d.Height += font->getKerningHeight();
// justification
switch (HAlign)
{
case EGUIA_CENTER:
// align to h centre
CurrentTextRect.UpperLeftCorner.X = (FrameRect.getWidth()/2) - (d.Width/2);
CurrentTextRect.LowerRightCorner.X = (FrameRect.getWidth()/2) + (d.Width/2);
break;
case EGUIA_LOWERRIGHT:
// align to right edge
CurrentTextRect.UpperLeftCorner.X = FrameRect.getWidth() - d.Width;
CurrentTextRect.LowerRightCorner.X = FrameRect.getWidth();
break;
default:
// align to left edge
CurrentTextRect.UpperLeftCorner.X = 0;
CurrentTextRect.LowerRightCorner.X = d.Width;
}
switch (VAlign)
{
case EGUIA_CENTER:
// align to v centre
CurrentTextRect.UpperLeftCorner.Y =
(FrameRect.getHeight()/2) - (lineCount*d.Height)/2 + d.Height*line;
break;
case EGUIA_LOWERRIGHT:
// align to bottom edge
CurrentTextRect.UpperLeftCorner.Y =
FrameRect.getHeight() - lineCount*d.Height + d.Height*line;
break;
default:
// align to top edge
CurrentTextRect.UpperLeftCorner.Y = d.Height*line;
break;
}
CurrentTextRect.UpperLeftCorner.X -= HScrollPos;
CurrentTextRect.LowerRightCorner.X -= HScrollPos;
CurrentTextRect.UpperLeftCorner.Y -= VScrollPos;
CurrentTextRect.LowerRightCorner.Y = CurrentTextRect.UpperLeftCorner.Y + d.Height;
CurrentTextRect += FrameRect.UpperLeftCorner;
}
s32 CGUIEditBox::getLineFromPos(s32 pos)
{
if (!WordWrap && !MultiLine)
return 0;
s32 i=0;
while (i < (s32)BrokenTextPositions.size())
{
if (BrokenTextPositions[i] > pos)
return i-1;
++i;
}
return (s32)BrokenTextPositions.size() - 1;
}
void CGUIEditBox::inputChar(wchar_t c)
{
if (!IsEnabled)
return;
if (c != 0)
{
if (Text.size() < Max || Max == 0)
{
core::stringw s;
if (MarkBegin != MarkEnd)
{
// replace marked text
const s32 realmbgn = MarkBegin < MarkEnd ? MarkBegin : MarkEnd;
const s32 realmend = MarkBegin < MarkEnd ? MarkEnd : MarkBegin;
s = Text.subString(0, realmbgn);
s.append(c);
s.append( Text.subString(realmend, Text.size()-realmend) );
Text = s;
CursorPos = realmbgn+1;
}
else
{
// add new character
s = Text.subString(0, CursorPos);
s.append(c);
s.append( Text.subString(CursorPos, Text.size()-CursorPos) );
Text = s;
++CursorPos;
}
BlinkStartTime = os::Timer::getTime();
setTextMarkers(0, 0);
}
}
breakText();
sendGuiEvent(EGET_EDITBOX_CHANGED);
calculateScrollPos();
}
void CGUIEditBox::calculateScrollPos()
{
if (!AutoScroll)
return;
// calculate horizontal scroll position
s32 cursLine = getLineFromPos(CursorPos);
setTextRect(cursLine);
// don't do horizontal scrolling when wordwrap is enabled.
if (!WordWrap)
{
// get cursor position
IGUISkin* skin = Environment->getSkin();
if (!skin)
return;
IGUIFont* font = OverrideFont ? OverrideFont : skin->getFont();
if (!font)
return;
core::stringw *txtLine = MultiLine ? &BrokenText[cursLine] : &Text;
s32 cPos = MultiLine ? CursorPos - BrokenTextPositions[cursLine] : CursorPos;
s32 cStart = CurrentTextRect.UpperLeftCorner.X + HScrollPos +
font->getDimension(txtLine->subString(0, cPos).c_str()).Width;
s32 cEnd = cStart + font->getDimension(L"_ ").Width;
if (FrameRect.LowerRightCorner.X < cEnd)
HScrollPos = cEnd - FrameRect.LowerRightCorner.X;
else if (FrameRect.UpperLeftCorner.X > cStart)
HScrollPos = cStart - FrameRect.UpperLeftCorner.X;
else
HScrollPos = 0;
// todo: adjust scrollbar
}
// vertical scroll position
if (FrameRect.LowerRightCorner.Y < CurrentTextRect.LowerRightCorner.Y + VScrollPos)
VScrollPos = CurrentTextRect.LowerRightCorner.Y - FrameRect.LowerRightCorner.Y + VScrollPos;
else if (FrameRect.UpperLeftCorner.Y > CurrentTextRect.UpperLeftCorner.Y + VScrollPos)
VScrollPos = CurrentTextRect.UpperLeftCorner.Y - FrameRect.UpperLeftCorner.Y + VScrollPos;
else
VScrollPos = 0;
// todo: adjust scrollbar
}
//! set text markers
void CGUIEditBox::setTextMarkers(s32 begin, s32 end)
{
if ( begin != MarkBegin || end != MarkEnd )
{
MarkBegin = begin;
MarkEnd = end;
sendGuiEvent(EGET_EDITBOX_MARKING_CHANGED);
}
}
//! send some gui event to parent
void CGUIEditBox::sendGuiEvent(EGUI_EVENT_TYPE type)
{
if ( Parent )
{
SEvent e;
e.EventType = EET_GUI_EVENT;
e.GUIEvent.Caller = this;
e.GUIEvent.Element = 0;
e.GUIEvent.EventType = type;
Parent->OnEvent(e);
}
}
//! Writes attributes of the element.
void CGUIEditBox::serializeAttributes(io::IAttributes* out, io::SAttributeReadWriteOptions* options=0) const
{
// IGUIEditBox::serializeAttributes(out,options);
out->addBool ("OverrideColorEnabled",OverrideColorEnabled );
out->addColor ("OverrideColor", OverrideColor);
// out->addFont("OverrideFont",OverrideFont);
out->addInt ("MaxChars", Max);
out->addBool ("WordWrap", WordWrap);
out->addBool ("MultiLine", MultiLine);
out->addBool ("AutoScroll", AutoScroll);
out->addBool ("PasswordBox", PasswordBox);
core::stringw ch = L" ";
ch[0] = PasswordChar;
out->addString("PasswordChar", ch.c_str());
out->addEnum ("HTextAlign", HAlign, GUIAlignmentNames);
out->addEnum ("VTextAlign", VAlign, GUIAlignmentNames);
IGUIEditBox::serializeAttributes(out,options);
}
//! Reads attributes of the element
void CGUIEditBox::deserializeAttributes(io::IAttributes* in, io::SAttributeReadWriteOptions* options=0)
{
IGUIEditBox::deserializeAttributes(in,options);
setOverrideColor(in->getAttributeAsColor("OverrideColor"));
enableOverrideColor(in->getAttributeAsBool("OverrideColorEnabled"));
setMax(in->getAttributeAsInt("MaxChars"));
setWordWrap(in->getAttributeAsBool("WordWrap"));
setMultiLine(in->getAttributeAsBool("MultiLine"));
setAutoScroll(in->getAttributeAsBool("AutoScroll"));
core::stringw ch = in->getAttributeAsStringW("PasswordChar");
if (!ch.size())
setPasswordBox(in->getAttributeAsBool("PasswordBox"));
else
setPasswordBox(in->getAttributeAsBool("PasswordBox"), ch[0]);
setTextAlignment( (EGUI_ALIGNMENT) in->getAttributeAsEnumeration("HTextAlign", GUIAlignmentNames),
(EGUI_ALIGNMENT) in->getAttributeAsEnumeration("VTextAlign", GUIAlignmentNames));
// setOverrideFont(in->getAttributeAsFont("OverrideFont"));
}
} // end namespace gui
} // end namespace irr
#endif // _IRR_COMPILE_WITH_GUI_
// Copyright (C) 2002-2010 Nikolaus Gebhardt
// This file is part of the "Irrlicht Engine".
// For conditions of distribution and use, see copyright notice in irrlicht.h
#ifndef __C_GUI_EDIT_BOX_H_INCLUDED__
#define __C_GUI_EDIT_BOX_H_INCLUDED__
#include "IrrCompileConfig.h"
#ifdef _IRR_COMPILE_WITH_GUI_
#include "IGUIEditBox.h"
#include "irrArray.h"
#include "IOSOperator.h"
namespace irr
{
namespace gui
{
class CGUIEditBox : public IGUIEditBox
{
public:
//! constructor
CGUIEditBox(const wchar_t* text, bool border, IGUIEnvironment* environment,
IGUIElement* parent, s32 id, const core::rect<s32>& rectangle);
//! destructor
virtual ~CGUIEditBox();
//! Sets another skin independent font.
virtual void setOverrideFont(IGUIFont* font=0);
//! Sets another color for the text.
virtual void setOverrideColor(video::SColor color);
//! Sets if the text should use the overide color or the
//! color in the gui skin.
virtual void enableOverrideColor(bool enable);
//! Turns the border on or off
virtual void setDrawBorder(bool border);
//! Enables or disables word wrap for using the edit box as multiline text editor.
virtual void setWordWrap(bool enable);
//! Checks if word wrap is enabled
//! \return true if word wrap is enabled, false otherwise
virtual bool isWordWrapEnabled() const;
//! Enables or disables newlines.
/** \param enable: If set to true, the EGET_EDITBOX_ENTER event will not be fired,
instead a newline character will be inserted. */
virtual void setMultiLine(bool enable);
//! Checks if multi line editing is enabled
//! \return true if mult-line is enabled, false otherwise
virtual bool isMultiLineEnabled() const;
//! Enables or disables automatic scrolling with cursor position
//! \param enable: If set to true, the text will move around with the cursor position
virtual void setAutoScroll(bool enable);
//! Checks to see if automatic scrolling is enabled
//! \return true if automatic scrolling is enabled, false if not
virtual bool isAutoScrollEnabled() const;
//! Gets the size area of the text in the edit box
//! \return Returns the size in pixels of the text
virtual core::dimension2du getTextDimension();
//! Sets text justification
virtual void setTextAlignment(EGUI_ALIGNMENT horizontal, EGUI_ALIGNMENT vertical);
//! called if an event happened.
virtual bool OnEvent(const SEvent& event);
//! draws the element and its children
virtual void draw();
//! Sets the new caption of this element.
virtual void setText(const wchar_t* text);
//! Sets the maximum amount of characters which may be entered in the box.
//! \param max: Maximum amount of characters. If 0, the character amount is
//! infinity.
virtual void setMax(u32 max);
//! Returns maximum amount of characters, previously set by setMax();
virtual u32 getMax() const;
//! Sets whether the edit box is a password box. Setting this to true will
/** disable MultiLine, WordWrap and the ability to copy with ctrl+c or ctrl+x
\param passwordBox: true to enable password, false to disable
\param passwordChar: the character that is displayed instead of letters */
virtual void setPasswordBox(bool passwordBox, wchar_t passwordChar = L'*');
//! Returns true if the edit box is currently a password box.
virtual bool isPasswordBox() const;
//! Updates the absolute position, splits text if required
virtual void updateAbsolutePosition();
//! Writes attributes of the element.
virtual void serializeAttributes(io::IAttributes* out, io::SAttributeReadWriteOptions* options) const;
//! Reads attributes of the element
virtual void deserializeAttributes(io::IAttributes* in, io::SAttributeReadWriteOptions* options);
protected:
//! Breaks the single text line.
void breakText();
//! sets the area of the given line
void setTextRect(s32 line);
//! returns the line number that the cursor is on
s32 getLineFromPos(s32 pos);
//! adds a letter to the edit box
void inputChar(wchar_t c);
//! calculates the current scroll position
void calculateScrollPos();
//! send some gui event to parent
void sendGuiEvent(EGUI_EVENT_TYPE type);
//! set text markers
void setTextMarkers(s32 begin, s32 end);
bool processKey(const SEvent& event);
bool processMouse(const SEvent& event);
s32 getCursorPos(s32 x, s32 y);
bool MouseMarking;
bool Border;
bool OverrideColorEnabled;
s32 MarkBegin;
s32 MarkEnd;
video::SColor OverrideColor;
gui::IGUIFont *OverrideFont, *LastBreakFont;
IOSOperator* Operator;
u32 BlinkStartTime;
s32 CursorPos;
s32 HScrollPos, VScrollPos; // scroll position in characters
u32 Max;
bool WordWrap, MultiLine, AutoScroll, PasswordBox;
wchar_t PasswordChar;
EGUI_ALIGNMENT HAlign, VAlign;
core::array< core::stringw > BrokenText;
core::array< s32 > BrokenTextPositions;
core::rect<s32> CurrentTextRect, FrameRect; // temporary values
};
} // end namespace gui
} // end namespace irr
#endif // _IRR_COMPILE_WITH_GUI_
#endif // __C_GUI_EDIT_BOX_H_INCLUDED__
// Copyright (C) 2002-2010 Nikolaus Gebhardt
// This file is part of the "Irrlicht Engine".
// For conditions of distribution and use, see copyright notice in irrlicht.h
#ifndef __C_IRR_DEVICE_STUB_H_INCLUDED__
#define __C_IRR_DEVICE_STUB_H_INCLUDED__
#include "IrrlichtDevice.h"
#include "IImagePresenter.h"
#include "SIrrCreationParameters.h"
#include "CVideoModeList.h"
namespace irr
{
// lots of prototypes:
class ILogger;
class CLogger;
namespace gui
{
class IGUIEnvironment;
IGUIEnvironment* createGUIEnvironment(io::IFileSystem* fs,
video::IVideoDriver* Driver, IOSOperator* op);
}
namespace scene
{
ISceneManager* createSceneManager(video::IVideoDriver* driver,
io::IFileSystem* fs, gui::ICursorControl* cc, gui::IGUIEnvironment *gui);
}
namespace io
{
IFileSystem* createFileSystem();
}
namespace video
{
IVideoDriver* createSoftwareDriver(const core::dimension2d<u32>& windowSize,
bool fullscreen, io::IFileSystem* io,
video::IImagePresenter* presenter);
IVideoDriver* createSoftwareDriver2(const core::dimension2d<u32>& windowSize,
bool fullscreen, io::IFileSystem* io,
video::IImagePresenter* presenter);
IVideoDriver* createNullDriver(io::IFileSystem* io, const core::dimension2d<u32>& screenSize);
}
//! Stub for an Irrlicht Device implementation
class CIrrDeviceStub : public IrrlichtDevice
{
public:
//! constructor
CIrrDeviceStub(const SIrrlichtCreationParameters& param);
//! destructor
virtual ~CIrrDeviceStub();
//! returns the video driver
virtual video::IVideoDriver* getVideoDriver();
//! return file system
virtual io::IFileSystem* getFileSystem();
//! returns the gui environment
virtual gui::IGUIEnvironment* getGUIEnvironment();
//! returns the scene manager
virtual scene::ISceneManager* getSceneManager();
//! \return Returns a pointer to the mouse cursor control interface.
virtual gui::ICursorControl* getCursorControl();
//! Returns a pointer to a list with all video modes supported by the gfx adapter.
virtual video::IVideoModeList* getVideoModeList();
//! Returns a pointer to the ITimer object. With it the current Time can be received.
virtual ITimer* getTimer();
//! Returns the version of the engine.
virtual const char* getVersion() const;
//! send the event to the right receiver
virtual bool postEventFromUser(const SEvent& event);
//! Sets a new event receiver to receive events
virtual void setEventReceiver(IEventReceiver* receiver);
//! Returns pointer to the current event receiver. Returns 0 if there is none.
virtual IEventReceiver* getEventReceiver();
//! Sets the input receiving scene manager.
/** If set to null, the main scene manager (returned by GetSceneManager()) will receive the input */
virtual void setInputReceivingSceneManager(scene::ISceneManager* sceneManager);
//! Returns a pointer to the logger.
virtual ILogger* getLogger();
//! Returns the operation system opertator object.
virtual IOSOperator* getOSOperator();
//! Checks if the window is running in fullscreen mode.
virtual bool isFullscreen() const;
//! get color format of the current window
virtual video::ECOLOR_FORMAT getColorFormat() const;
//! Activate any joysticks, and generate events for them.
virtual bool activateJoysticks(core::array<SJoystickInfo> & joystickInfo);
//! Set the current Gamma Value for the Display
virtual bool setGammaRamp( f32 red, f32 green, f32 blue, f32 brightness, f32 contrast );
//! Get the current Gamma Value for the Display
virtual bool getGammaRamp( f32 &red, f32 &green, f32 &blue, f32 &brightness, f32 &contrast );
//! Set the maximal elapsed time between 2 clicks to generate doubleclicks for the mouse. It also affects tripleclick behaviour.
//! When set to 0 no double- and tripleclicks will be generated.
virtual void setDoubleClickTime( u32 timeMs );
//! Get the maximal elapsed time between 2 clicks to generate double- and tripleclicks for the mouse.
virtual u32 getDoubleClickTime() const;
//! Remove all messages pending in the system message loop
virtual void clearSystemMessages();
protected:
void createGUIAndScene();
//! checks version of SDK and prints warning if there might be a problem
bool checkVersion(const char* version);
//! Compares to the last call of this function to return double and triple clicks.
//! \return Returns only 1,2 or 3. A 4th click will start with 1 again.
virtual u32 checkSuccessiveClicks(s32 mouseX, s32 mouseY, EMOUSE_INPUT_EVENT inputEvent );
void calculateGammaRamp ( u16 *ramp, f32 gamma, f32 relativebrightness, f32 relativecontrast );
void calculateGammaFromRamp ( f32 &gamma, const u16 *ramp );
video::IVideoDriver* VideoDriver;
gui::IGUIEnvironment* GUIEnvironment;
scene::ISceneManager* SceneManager;
ITimer* Timer;
gui::ICursorControl* CursorControl;
IEventReceiver* UserReceiver;
CLogger* Logger;
IOSOperator* Operator;
io::IFileSystem* FileSystem;
scene::ISceneManager* InputReceivingSceneManager;
struct SMouseMultiClicks
{
SMouseMultiClicks()
: DoubleClickTime(500), CountSuccessiveClicks(0), LastClickTime(0), LastMouseInputEvent(EMIE_COUNT)
{}
u32 DoubleClickTime;
u32 CountSuccessiveClicks;
u32 LastClickTime;
core::position2di LastClick;
EMOUSE_INPUT_EVENT LastMouseInputEvent;
};
SMouseMultiClicks MouseMultiClicks;
video::CVideoModeList VideoModeList;
SIrrlichtCreationParameters CreationParams;
bool Close;
};
} // end namespace irr
#endif
// Copyright (C) 2002-2010 Nikolaus Gebhardt
// This file is part of the "Irrlicht Engine".
// For conditions of distribution and use, see copyright notice in irrlicht.h
#ifdef _WIN32
#include "IrrCompileConfig.h"
#ifdef _IRR_COMPILE_WITH_WINDOWS_DEVICE_
#include "CIrrDeviceWin32.h"
#include "IEventReceiver.h"
#include "IGUIElement.h"
#include "IGUIEnvironment.h"
#include "IGUIEditBox.h"
#include "irrList.h"
#include "os.h"
#include "CTimer.h"
#include "irrString.h"
#include "COSOperator.h"
#include "dimension2d.h"
#include <winuser.h>
#include <stdlib.h>
#include <locale.h>
#include <imm.h>
namespace irr {
namespace video {
#ifdef _IRR_COMPILE_WITH_DIRECT3D_8_
IVideoDriver* createDirectX8Driver(const core::dimension2d<u32>& screenSize, HWND window,
u32 bits, bool fullscreen, bool stencilbuffer, io::IFileSystem* io,
bool pureSoftware, bool highPrecisionFPU, bool vsync, u8 antiAlias);
#endif
#ifdef _IRR_COMPILE_WITH_DIRECT3D_9_
IVideoDriver* createDirectX9Driver(const core::dimension2d<u32>& screenSize, HWND window,
u32 bits, bool fullscreen, bool stencilbuffer, io::IFileSystem* io,
bool pureSoftware, bool highPrecisionFPU, bool vsync, u8 antiAlias);
#endif
#ifdef _IRR_COMPILE_WITH_OPENGL_
IVideoDriver* createOpenGLDriver(const irr::SIrrlichtCreationParameters& params,
io::IFileSystem* io, CIrrDeviceWin32* device);
#endif
}
} // end namespace irr
// Get the codepage from the locale language id
// Based on the table from http://www.science.co.il/Language/Locale-Codes.asp?s=decimal
static unsigned int LocaleIdToCodepage(unsigned int lcid) {
switch ( lcid ) {
case 1098: // Telugu
case 1095: // Gujarati
case 1094: // Punjabi
case 1103: // Sanskrit
case 1111: // Konkani
case 1114: // Syriac
case 1099: // Kannada
case 1102: // Marathi
case 1125: // Divehi
case 1067: // Armenian
case 1081: // Hindi
case 1079: // Georgian
case 1097: // Tamil
return 0;
case 1054: // Thai
return 874;
case 1041: // Japanese
return 932;
case 2052: // Chinese (PRC)
case 4100: // Chinese (Singapore)
return 936;
case 1042: // Korean
return 949;
case 5124: // Chinese (Macau S.A.R.)
case 3076: // Chinese (Hong Kong S.A.R.)
case 1028: // Chinese (Taiwan)
return 950;
case 1048: // Romanian
case 1060: // Slovenian
case 1038: // Hungarian
case 1051: // Slovak
case 1045: // Polish
case 1052: // Albanian
case 2074: // Serbian (Latin)
case 1050: // Croatian
case 1029: // Czech
return 1250;
case 1104: // Mongolian (Cyrillic)
case 1071: // FYRO Macedonian
case 2115: // Uzbek (Cyrillic)
case 1058: // Ukrainian
case 2092: // Azeri (Cyrillic)
case 1092: // Tatar
case 1087: // Kazakh
case 1059: // Belarusian
case 1088: // Kyrgyz (Cyrillic)
case 1026: // Bulgarian
case 3098: // Serbian (Cyrillic)
case 1049: // Russian
return 1251;
case 8201: // English (Jamaica)
case 3084: // French (Canada)
case 1036: // French (France)
case 5132: // French (Luxembourg)
case 5129: // English (New Zealand)
case 6153: // English (Ireland)
case 1043: // Dutch (Netherlands)
case 9225: // English (Caribbean)
case 4108: // French (Switzerland)
case 4105: // English (Canada)
case 1110: // Galician
case 10249: // English (Belize)
case 3079: // German (Austria)
case 6156: // French (Monaco)
case 12297: // English (Zimbabwe)
case 1069: // Basque
case 2067: // Dutch (Belgium)
case 2060: // French (Belgium)
case 1035: // Finnish
case 1080: // Faroese
case 1031: // German (Germany)
case 3081: // English (Australia)
case 1033: // English (United States)
case 2057: // English (United Kingdom)
case 1027: // Catalan
case 11273: // English (Trinidad)
case 7177: // English (South Africa)
case 1030: // Danish
case 13321: // English (Philippines)
case 15370: // Spanish (Paraguay)
case 9226: // Spanish (Colombia)
case 5130: // Spanish (Costa Rica)
case 7178: // Spanish (Dominican Republic)
case 12298: // Spanish (Ecuador)
case 17418: // Spanish (El Salvador)
case 4106: // Spanish (Guatemala)
case 18442: // Spanish (Honduras)
case 3082: // Spanish (International Sort)
case 13322: // Spanish (Chile)
case 19466: // Spanish (Nicaragua)
case 2058: // Spanish (Mexico)
case 10250: // Spanish (Peru)
case 20490: // Spanish (Puerto Rico)
case 1034: // Spanish (Traditional Sort)
case 14346: // Spanish (Uruguay)
case 8202: // Spanish (Venezuela)
case 1089: // Swahili
case 1053: // Swedish
case 2077: // Swedish (Finland)
case 5127: // German (Liechtenstein)
case 1078: // Afrikaans
case 6154: // Spanish (Panama)
case 4103: // German (Luxembourg)
case 16394: // Spanish (Bolivia)
case 2055: // German (Switzerland)
case 1039: // Icelandic
case 1057: // Indonesian
case 1040: // Italian (Italy)
case 2064: // Italian (Switzerland)
case 2068: // Norwegian (Nynorsk)
case 11274: // Spanish (Argentina)
case 1046: // Portuguese (Brazil)
case 1044: // Norwegian (Bokmal)
case 1086: // Malay (Malaysia)
case 2110: // Malay (Brunei Darussalam)
case 2070: // Portuguese (Portugal)
return 1252;
case 1032: // Greek
return 1253;
case 1091: // Uzbek (Latin)
case 1068: // Azeri (Latin)
case 1055: // Turkish
return 1254;
case 1037: // Hebrew
return 1255;
case 5121: // Arabic (Algeria)
case 15361: // Arabic (Bahrain)
case 9217: // Arabic (Yemen)
case 3073: // Arabic (Egypt)
case 2049: // Arabic (Iraq)
case 11265: // Arabic (Jordan)
case 13313: // Arabic (Kuwait)
case 12289: // Arabic (Lebanon)
case 4097: // Arabic (Libya)
case 6145: // Arabic (Morocco)
case 8193: // Arabic (Oman)
case 16385: // Arabic (Qatar)
case 1025: // Arabic (Saudi Arabia)
case 10241: // Arabic (Syria)
case 14337: // Arabic (U.A.E.)
case 1065: // Farsi
case 1056: // Urdu
case 7169: // Arabic (Tunisia)
return 1256;
case 1061: // Estonian
case 1062: // Latvian
case 1063: // Lithuanian
return 1257;
case 1066: // Vietnamese
return 1258;
}
return 65001; // utf-8
}
namespace {
struct SEnvMapper {
HWND hWnd;
irr::CIrrDeviceWin32* irrDev;
};
irr::core::list<SEnvMapper> EnvMap;
HKL KEYBOARD_INPUT_HKL = 0;
unsigned int KEYBOARD_INPUT_CODEPAGE = 1252;
};
SEnvMapper* getEnvMapperFromHWnd(HWND hWnd) {
irr::core::list<SEnvMapper>::Iterator it = EnvMap.begin();
for (; it != EnvMap.end(); ++it)
if ((*it).hWnd == hWnd)
return &(*it);
return 0;
}
irr::CIrrDeviceWin32* getDeviceFromHWnd(HWND hWnd) {
irr::core::list<SEnvMapper>::Iterator it = EnvMap.begin();
for (; it != EnvMap.end(); ++it)
if ((*it).hWnd == hWnd)
return (*it).irrDev;
return 0;
}
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) {
#ifndef WM_MOUSEWHEEL
#define WM_MOUSEWHEEL 0x020A
#endif
#ifndef WHEEL_DELTA
#define WHEEL_DELTA 120
#endif
irr::CIrrDeviceWin32* dev = 0;
irr::SEvent event;
static irr::s32 ClickCount = 0;
if (GetCapture() != hWnd && ClickCount > 0)
ClickCount = 0;
struct messageMap {
irr::s32 group;
UINT winMessage;
irr::s32 irrMessage;
};
static messageMap mouseMap[] = {
{0, WM_LBUTTONDOWN, irr::EMIE_LMOUSE_PRESSED_DOWN},
{1, WM_LBUTTONUP, irr::EMIE_LMOUSE_LEFT_UP},
{0, WM_RBUTTONDOWN, irr::EMIE_RMOUSE_PRESSED_DOWN},
{1, WM_RBUTTONUP, irr::EMIE_RMOUSE_LEFT_UP},
{0, WM_MBUTTONDOWN, irr::EMIE_MMOUSE_PRESSED_DOWN},
{1, WM_MBUTTONUP, irr::EMIE_MMOUSE_LEFT_UP},
{2, WM_MOUSEMOVE, irr::EMIE_MOUSE_MOVED},
{3, WM_MOUSEWHEEL, irr::EMIE_MOUSE_WHEEL},
{ -1, 0, 0}
};
// handle grouped events
messageMap * m = mouseMap;
while ( m->group >= 0 && m->winMessage != message )
m += 1;
if ( m->group >= 0 ) {
if ( m->group == 0 ) { // down
ClickCount++;
SetCapture(hWnd);
} else if ( m->group == 1 ) { // up
ClickCount--;
if (ClickCount < 1) {
ClickCount = 0;
ReleaseCapture();
}
}
event.EventType = irr::EET_MOUSE_INPUT_EVENT;
event.MouseInput.Event = (irr::EMOUSE_INPUT_EVENT) m->irrMessage;
event.MouseInput.X = (short)LOWORD(lParam);
event.MouseInput.Y = (short)HIWORD(lParam);
event.MouseInput.Shift = ((LOWORD(wParam) & MK_SHIFT) != 0);
event.MouseInput.Control = ((LOWORD(wParam) & MK_CONTROL) != 0);
// left and right mouse buttons
event.MouseInput.ButtonStates = wParam & ( MK_LBUTTON | MK_RBUTTON);
// middle and extra buttons
if (wParam & MK_MBUTTON)
event.MouseInput.ButtonStates |= irr::EMBSM_MIDDLE;
#if(_WIN32_WINNT >= 0x0500)
if (wParam & MK_XBUTTON1)
event.MouseInput.ButtonStates |= irr::EMBSM_EXTRA1;
if (wParam & MK_XBUTTON2)
event.MouseInput.ButtonStates |= irr::EMBSM_EXTRA2;
#endif
event.MouseInput.Wheel = 0.f;
// wheel
if ( m->group == 3 ) {
POINT p; // fixed by jox
p.x = 0;
p.y = 0;
ClientToScreen(hWnd, &p);
event.MouseInput.X -= p.x;
event.MouseInput.Y -= p.y;
event.MouseInput.Wheel = ((irr::f32)((short)HIWORD(wParam))) / (irr::f32)WHEEL_DELTA;
}
dev = getDeviceFromHWnd(hWnd);
if (dev) {
dev->postEventFromUser(event);
if ( event.MouseInput.Event >= irr::EMIE_LMOUSE_PRESSED_DOWN && event.MouseInput.Event <= irr::EMIE_MMOUSE_PRESSED_DOWN ) {
irr::u32 clicks = dev->checkSuccessiveClicks(event.MouseInput.X, event.MouseInput.Y, event.MouseInput.Event);
if ( clicks == 2 ) {
event.MouseInput.Event = (irr::EMOUSE_INPUT_EVENT)(irr::EMIE_LMOUSE_DOUBLE_CLICK + event.MouseInput.Event - irr::EMIE_LMOUSE_PRESSED_DOWN);
dev->postEventFromUser(event);
} else if ( clicks == 3 ) {
event.MouseInput.Event = (irr::EMOUSE_INPUT_EVENT)(irr::EMIE_LMOUSE_TRIPLE_CLICK + event.MouseInput.Event - irr::EMIE_LMOUSE_PRESSED_DOWN);
dev->postEventFromUser(event);
}
}
}
return 0;
}
switch (message) {
case WM_PAINT: {
PAINTSTRUCT ps;
BeginPaint(hWnd, &ps);
EndPaint(hWnd, &ps);
}
return 0;
case WM_ERASEBKGND:
return 0;
case WM_SYSKEYDOWN:
case WM_SYSKEYUP:
case WM_KEYDOWN:
case WM_KEYUP: {
BYTE allKeys[256];
event.EventType = irr::EET_KEY_INPUT_EVENT;
event.KeyInput.Key = (irr::EKEY_CODE)wParam;
event.KeyInput.PressedDown = (message == WM_KEYDOWN || message == WM_SYSKEYDOWN);
const UINT MY_MAPVK_VSC_TO_VK_EX = 3; // MAPVK_VSC_TO_VK_EX should be in SDK according to MSDN, but isn't in mine.
if ( event.KeyInput.Key == irr::KEY_SHIFT ) {
// this will fail on systems before windows NT/2000/XP, not sure _what_ will return there instead.
event.KeyInput.Key = (irr::EKEY_CODE)MapVirtualKey( ((lParam >> 16) & 255), MY_MAPVK_VSC_TO_VK_EX );
}
if ( event.KeyInput.Key == irr::KEY_CONTROL ) {
event.KeyInput.Key = (irr::EKEY_CODE)MapVirtualKey( ((lParam >> 16) & 255), MY_MAPVK_VSC_TO_VK_EX );
// some keyboards will just return LEFT for both - left and right keys. So also check extend bit.
if (lParam & 0x1000000)
event.KeyInput.Key = irr::KEY_RCONTROL;
}
if ( event.KeyInput.Key == irr::KEY_MENU ) {
event.KeyInput.Key = (irr::EKEY_CODE)MapVirtualKey( ((lParam >> 16) & 255), MY_MAPVK_VSC_TO_VK_EX );
if (lParam & 0x1000000)
event.KeyInput.Key = irr::KEY_RMENU;
}
GetKeyboardState(allKeys);
event.KeyInput.Shift = ((allKeys[VK_SHIFT] & 0x80) != 0);
event.KeyInput.Control = ((allKeys[VK_CONTROL] & 0x80) != 0);
// Handle unicode and deadkeys in a way that works since Windows 95 and nt4.0
// Using ToUnicode instead would be shorter, but would to my knowledge not run on 95 and 98.
WORD keyChars[2];
UINT scanCode = HIWORD(lParam);
int conversionResult = ToAsciiEx(wParam, scanCode, allKeys, keyChars, 0, KEYBOARD_INPUT_HKL);
if (conversionResult == 1) {
WORD unicodeChar;
MultiByteToWideChar(
KEYBOARD_INPUT_CODEPAGE,
MB_PRECOMPOSED, // default
(LPCSTR)keyChars,
sizeof(keyChars),
(WCHAR*)&unicodeChar,
1 );
event.KeyInput.Char = unicodeChar;
} else
event.KeyInput.Char = 0;
// allow composing characters like '@' with Alt Gr on non-US keyboards
if ((allKeys[VK_MENU] & 0x80) != 0)
event.KeyInput.Control = 0;
dev = getDeviceFromHWnd(hWnd);
if (dev)
dev->postEventFromUser(event);
if (message == WM_SYSKEYDOWN || message == WM_SYSKEYUP)
return DefWindowProc(hWnd, message, wParam, lParam);
else
return 0;
}
case WM_SIZE: {
// resize
dev = getDeviceFromHWnd(hWnd);
if (dev)
dev->OnResized();
}
return 0;
case WM_DESTROY:
PostQuitMessage(0);
return 0;
case WM_SYSCOMMAND:
// prevent screensaver or monitor powersave mode from starting
if ((wParam & 0xFFF0) == SC_SCREENSAVE ||
(wParam & 0xFFF0) == SC_MONITORPOWER)
return 0;
break;
case WM_ACTIVATE:
// we need to take care for screen changes, e.g. Alt-Tab
dev = getDeviceFromHWnd(hWnd);
if (dev) {
if ((wParam & 0xFF) == WA_INACTIVE)
dev->switchToFullScreen(true);
else
dev->switchToFullScreen();
}
break;
case WM_USER:
event.EventType = irr::EET_USER_EVENT;
event.UserEvent.UserData1 = (irr::s32)wParam;
event.UserEvent.UserData2 = (irr::s32)lParam;
dev = getDeviceFromHWnd(hWnd);
if (dev)
dev->postEventFromUser(event);
return 0;
case WM_SETCURSOR:
// because Windows forgot about that in the meantime
dev = getDeviceFromHWnd(hWnd);
if (dev)
dev->getCursorControl()->setVisible( dev->getCursorControl()->isVisible() );
break;
case WM_INPUTLANGCHANGE:
// get the new codepage used for keyboard input
KEYBOARD_INPUT_HKL = GetKeyboardLayout(0);
KEYBOARD_INPUT_CODEPAGE = LocaleIdToCodepage( LOWORD(KEYBOARD_INPUT_HKL) );
return 0;
case WM_IME_STARTCOMPOSITION: {
dev = getDeviceFromHWnd(hWnd);
irr::gui::IGUIElement* ele = dev->getGUIEnvironment()->getFocus();
if(!ele)
break;
irr::core::position2di pos = ele->getAbsolutePosition().UpperLeftCorner;
CANDIDATEFORM cd;
cd.dwIndex = 0;
cd.dwStyle = CFS_CANDIDATEPOS;
cd.ptCurrentPos.x = pos.X;
cd.ptCurrentPos.y = pos.Y + 10;
SendMessage(ImmGetDefaultIMEWnd(hWnd), WM_IME_CONTROL, IMC_SETCANDIDATEPOS, (LPARAM)&cd);
break;
}
case WM_IME_CHAR: {
/* setlocale(LC_ALL, "");*/
event.EventType = irr::EET_KEY_INPUT_EVENT;
event.KeyInput.PressedDown = true;
dev = getDeviceFromHWnd(hWnd);
// unsigned char mbc[3];
// wchar_t wc[2];
// if(wParam > 255) {
// mbc[0] = wParam >> 8;
// mbc[1] = wParam & 0xff;
// mbc[2] = 0;
// } else {
// mbc[0] = wParam;
// mbc[1] = mbc[2] = 0;
// }
// mbstowcs(wc, (char *)&mbc, MB_CUR_MAX );
// event.KeyInput.Char = wc[0];
event.KeyInput.Char = wParam;
event.KeyInput.Key = irr::KEY_ACCEPT;
event.KeyInput.Shift = 0;
event.KeyInput.Control = 0;
if (dev)
dev->postEventFromUser(event);
return 0;
}
}
return DefWindowProc(hWnd, message, wParam, lParam);
}
namespace irr {
//! constructor
CIrrDeviceWin32::CIrrDeviceWin32(const SIrrlichtCreationParameters& params)
: CIrrDeviceStub(params), HWnd(0), ChangedToFullScreen(false),
IsNonNTWindows(false), Resized(false),
ExternalWindow(false), Win32CursorControl(0) {
#ifdef _DEBUG
setDebugName("CIrrDeviceWin32");
#endif
// get windows version and create OS operator
core::stringc winversion;
getWindowsVersion(winversion);
Operator = new COSOperator(winversion.c_str());
os::Printer::log(winversion.c_str(), ELL_INFORMATION);
// get handle to exe file
HINSTANCE hInstance = GetModuleHandle(0);
// create the window if we need to and we do not use the null device
if (!CreationParams.WindowId && CreationParams.DriverType != video::EDT_NULL) {
const fschar_t* ClassName = __TEXT("CIrrDeviceWin32");
// Register Class
WNDCLASSEX wcex;
wcex.cbSize = sizeof(WNDCLASSEX);
wcex.style = CS_HREDRAW | CS_VREDRAW;
wcex.lpfnWndProc = WndProc;
wcex.cbClsExtra = 0;
wcex.cbWndExtra = 0;
wcex.hInstance = hInstance;
wcex.hIcon = NULL;
wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
wcex.lpszMenuName = 0;
wcex.lpszClassName = ClassName;
wcex.hIconSm = 0;
// if there is an icon, load it
wcex.hIcon = (HICON)LoadImage(hInstance, __TEXT("irrlicht.ico"), IMAGE_ICON, 0, 0, LR_LOADFROMFILE);
RegisterClassEx(&wcex);
// calculate client size
RECT clientSize;
clientSize.top = 0;
clientSize.left = 0;
clientSize.right = CreationParams.WindowSize.Width;
clientSize.bottom = CreationParams.WindowSize.Height;
DWORD style = WS_POPUP;
if (!CreationParams.Fullscreen)
style = WS_SYSMENU | WS_BORDER | WS_CAPTION | WS_CLIPCHILDREN | WS_CLIPSIBLINGS;
AdjustWindowRect(&clientSize, style, FALSE);
const s32 realWidth = clientSize.right - clientSize.left;
const s32 realHeight = clientSize.bottom - clientSize.top;
s32 windowLeft = (GetSystemMetrics(SM_CXSCREEN) - realWidth) / 2;
s32 windowTop = (GetSystemMetrics(SM_CYSCREEN) - realHeight) / 2;
if ( windowLeft < 0 )
windowLeft = 0;
if ( windowTop < 0 )
windowTop = 0; // make sure window menus are in screen on creation
if (CreationParams.Fullscreen) {
windowLeft = 0;
windowTop = 0;
}
// create window
HWnd = CreateWindow( ClassName, __TEXT(""), style, windowLeft, windowTop,
realWidth, realHeight, NULL, NULL, hInstance, NULL);
CreationParams.WindowId = HWnd;
// CreationParams.WindowSize.Width = realWidth;
// CreationParams.WindowSize.Height = realHeight;
ShowWindow(HWnd, SW_SHOW);
UpdateWindow(HWnd);
// fix ugly ATI driver bugs. Thanks to ariaci
MoveWindow(HWnd, windowLeft, windowTop, realWidth, realHeight, TRUE);
// make sure everything gets updated to the real sizes
Resized = true;
} else if (CreationParams.WindowId) {
// attach external window
HWnd = static_cast<HWND>(CreationParams.WindowId);
RECT r;
GetWindowRect(HWnd, &r);
CreationParams.WindowSize.Width = r.right - r.left;
CreationParams.WindowSize.Height = r.bottom - r.top;
CreationParams.Fullscreen = false;
ExternalWindow = true;
}
// create cursor control
Win32CursorControl = new CCursorControl(CreationParams.WindowSize, HWnd, CreationParams.Fullscreen);
CursorControl = Win32CursorControl;
// initialize doubleclicks with system values
MouseMultiClicks.DoubleClickTime = GetDoubleClickTime();
// create driver
createDriver();
if (VideoDriver)
createGUIAndScene();
// register environment
SEnvMapper em;
em.irrDev = this;
em.hWnd = HWnd;
EnvMap.push_back(em);
// set this as active window
SetActiveWindow(HWnd);
SetForegroundWindow(HWnd);
// get the codepage used for keyboard input
KEYBOARD_INPUT_HKL = GetKeyboardLayout(0);
KEYBOARD_INPUT_CODEPAGE = LocaleIdToCodepage( LOWORD(KEYBOARD_INPUT_HKL) );
}
//! destructor
CIrrDeviceWin32::~CIrrDeviceWin32() {
// unregister environment
irr::core::list<SEnvMapper>::Iterator it = EnvMap.begin();
for (; it != EnvMap.end(); ++it) {
if ((*it).hWnd == HWnd) {
EnvMap.erase(it);
break;
}
}
switchToFullScreen(true);
}
//! create the driver
void CIrrDeviceWin32::createDriver() {
switch(CreationParams.DriverType) {
case video::EDT_DIRECT3D8:
#ifdef _IRR_COMPILE_WITH_DIRECT3D_8_
VideoDriver = video::createDirectX8Driver(CreationParams.WindowSize, HWnd,
CreationParams.Bits, CreationParams.Fullscreen, CreationParams.Stencilbuffer,
FileSystem, false, CreationParams.HighPrecisionFPU, CreationParams.Vsync,
CreationParams.AntiAlias);
if (!VideoDriver) {
os::Printer::log("Could not create DIRECT3D8 Driver.", ELL_ERROR);
}
#else
os::Printer::log("DIRECT3D8 Driver was not compiled into this dll. Try another one.", ELL_ERROR);
#endif // _IRR_COMPILE_WITH_DIRECT3D_8_
break;
case video::EDT_DIRECT3D9:
#ifdef _IRR_COMPILE_WITH_DIRECT3D_9_
VideoDriver = video::createDirectX9Driver(CreationParams.WindowSize, HWnd,
CreationParams.Bits, CreationParams.Fullscreen, CreationParams.Stencilbuffer,
FileSystem, false, CreationParams.HighPrecisionFPU, CreationParams.Vsync,
CreationParams.AntiAlias);
if (!VideoDriver) {
os::Printer::log("Could not create DIRECT3D9 Driver.", ELL_ERROR);
}
#else
os::Printer::log("DIRECT3D9 Driver was not compiled into this dll. Try another one.", ELL_ERROR);
#endif // _IRR_COMPILE_WITH_DIRECT3D_9_
break;
case video::EDT_OPENGL:
#ifdef _IRR_COMPILE_WITH_OPENGL_
switchToFullScreen();
VideoDriver = video::createOpenGLDriver(CreationParams, FileSystem, this);
if (!VideoDriver) {
os::Printer::log("Could not create OpenGL driver.", ELL_ERROR);
}
#else
os::Printer::log("OpenGL driver was not compiled in.", ELL_ERROR);
#endif
break;
case video::EDT_SOFTWARE:
#ifdef _IRR_COMPILE_WITH_SOFTWARE_
switchToFullScreen();
VideoDriver = video::createSoftwareDriver(CreationParams.WindowSize, CreationParams.Fullscreen, FileSystem, this);
#else
os::Printer::log("Software driver was not compiled in.", ELL_ERROR);
#endif
break;
case video::EDT_BURNINGSVIDEO:
#ifdef _IRR_COMPILE_WITH_BURNINGSVIDEO_
switchToFullScreen();
VideoDriver = video::createSoftwareDriver2(CreationParams.WindowSize, CreationParams.Fullscreen, FileSystem, this);
#else
os::Printer::log("Burning's Video driver was not compiled in.", ELL_ERROR);
#endif
break;
case video::EDT_NULL:
// create null driver
VideoDriver = video::createNullDriver(FileSystem, CreationParams.WindowSize);
break;
default:
os::Printer::log("Unable to create video driver of unknown type.", ELL_ERROR);
break;
}
}
//! runs the device. Returns false if device wants to be deleted
bool CIrrDeviceWin32::run() {
os::Timer::tick();
MSG msg;
while (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) {
// No message translation because we don't use WM_CHAR and it would conflict with our
// deadkey handling.
TranslateMessage(&msg);
if (ExternalWindow && msg.hwnd == HWnd)
WndProc(HWnd, msg.message, msg.wParam, msg.lParam);
else
DispatchMessage(&msg);
if (msg.message == WM_QUIT)
Close = true;
}
if (!Close)
resizeIfNecessary();
if(!Close)
pollJoysticks();
_IRR_IMPLEMENT_MANAGED_MARSHALLING_BUGFIX;
return !Close;
}
//! Pause the current process for the minimum time allowed only to allow other processes to execute
void CIrrDeviceWin32::yield() {
Sleep(1);
}
//! Pause execution and let other processes to run for a specified amount of time.
void CIrrDeviceWin32::sleep(u32 timeMs, bool pauseTimer) {
const bool wasStopped = Timer ? Timer->isStopped() : true;
if (pauseTimer && !wasStopped)
Timer->stop();
Sleep(timeMs);
if (pauseTimer && !wasStopped)
Timer->start();
}
void CIrrDeviceWin32::resizeIfNecessary() {
if (!Resized)
return;
RECT r;
GetClientRect(HWnd, &r);
char tmp[255];
if (r.right < 2 || r.bottom < 2) {
sprintf(tmp, "Ignoring resize operation to (%ld %ld)", r.right, r.bottom);
os::Printer::log(tmp);
} else {
sprintf(tmp, "Resizing window (%ld %ld)", r.right, r.bottom);
os::Printer::log(tmp);
getVideoDriver()->OnResize(irr::core::dimension2du((u32)r.right, (u32)r.bottom));
getWin32CursorControl()->OnResize(getVideoDriver()->getScreenSize());
}
Resized = false;
}
//! sets the caption of the window
void CIrrDeviceWin32::setWindowCaption(const wchar_t* text) {
DWORD dwResult;
if (IsNonNTWindows) {
const core::stringc s = text;
#if defined(_WIN64) || defined(WIN64)
SetWindowTextA(HWnd, s.c_str());
#else
SendMessageTimeout(HWnd, WM_SETTEXT, 0,
reinterpret_cast<LPARAM>(s.c_str()),
SMTO_ABORTIFHUNG, 2000, &dwResult);
#endif
} else {
#if defined(_WIN64) || defined(WIN64)
SetWindowTextW(HWnd, text);
#else
SendMessageTimeoutW(HWnd, WM_SETTEXT, 0,
reinterpret_cast<LPARAM>(text),
SMTO_ABORTIFHUNG, 2000, &dwResult);
#endif
}
}
//! presents a surface in the client area
bool CIrrDeviceWin32::present(video::IImage* image, void* windowId, core::rect<s32>* src) {
HWND hwnd = HWnd;
if ( windowId )
hwnd = reinterpret_cast<HWND>(windowId);
HDC dc = GetDC(hwnd);
if ( dc ) {
RECT rect;
GetClientRect(hwnd, &rect);
const void* memory = (const void *)image->lock();
BITMAPV4HEADER bi;
ZeroMemory (&bi, sizeof(bi));
bi.bV4Size = sizeof(BITMAPINFOHEADER);
bi.bV4BitCount = (WORD)image->getBitsPerPixel();
bi.bV4Planes = 1;
bi.bV4Width = image->getDimension().Width;
bi.bV4Height = -((s32)image->getDimension().Height);
bi.bV4V4Compression = BI_BITFIELDS;
bi.bV4AlphaMask = image->getAlphaMask();
bi.bV4RedMask = image->getRedMask();
bi.bV4GreenMask = image->getGreenMask();
bi.bV4BlueMask = image->getBlueMask();
if ( src ) {
StretchDIBits(dc, 0, 0, rect.right, rect.bottom,
src->UpperLeftCorner.X, src->UpperLeftCorner.Y,
src->getWidth(), src->getHeight(),
memory, (const BITMAPINFO*)(&bi), DIB_RGB_COLORS, SRCCOPY);
} else {
StretchDIBits(dc, 0, 0, rect.right, rect.bottom,
0, 0, image->getDimension().Width, image->getDimension().Height,
memory, (const BITMAPINFO*)(&bi), DIB_RGB_COLORS, SRCCOPY);
}
image->unlock();
ReleaseDC(hwnd, dc);
}
return true;
}
//! notifies the device that it should close itself
void CIrrDeviceWin32::closeDevice() {
MSG msg;
PeekMessage(&msg, NULL, WM_QUIT, WM_QUIT, PM_REMOVE);
PostQuitMessage(0);
PeekMessage(&msg, NULL, WM_QUIT, WM_QUIT, PM_REMOVE);
DestroyWindow(HWnd);
Close = true;
}
//! returns if window is active. if not, nothing needs to be drawn
bool CIrrDeviceWin32::isWindowActive() const {
_IRR_IMPLEMENT_MANAGED_MARSHALLING_BUGFIX;
return (GetActiveWindow() == HWnd);
}
//! returns if window has focus
bool CIrrDeviceWin32::isWindowFocused() const {
bool ret = (GetFocus() == HWnd);
_IRR_IMPLEMENT_MANAGED_MARSHALLING_BUGFIX;
return ret;
}
//! returns if window is minimized
bool CIrrDeviceWin32::isWindowMinimized() const {
WINDOWPLACEMENT plc;
plc.length = sizeof(WINDOWPLACEMENT);
bool ret = false;
if (GetWindowPlacement(HWnd, &plc))
ret = (plc.showCmd & SW_SHOWMINIMIZED) != 0;
_IRR_IMPLEMENT_MANAGED_MARSHALLING_BUGFIX;
return ret;
}
//! switches to fullscreen
bool CIrrDeviceWin32::switchToFullScreen(bool reset) {
if (!CreationParams.Fullscreen)
return true;
if (reset) {
if (ChangedToFullScreen)
return (ChangeDisplaySettings(NULL, 0) == DISP_CHANGE_SUCCESSFUL);
else
return true;
}
DEVMODE dm;
memset(&dm, 0, sizeof(dm));
dm.dmSize = sizeof(dm);
// use default values from current setting
EnumDisplaySettings(NULL, ENUM_CURRENT_SETTINGS, &dm);
dm.dmPelsWidth = CreationParams.WindowSize.Width;
dm.dmPelsHeight = CreationParams.WindowSize.Height;
dm.dmBitsPerPel = CreationParams.Bits;
dm.dmFields = DM_BITSPERPEL | DM_PELSWIDTH | DM_PELSHEIGHT | DM_DISPLAYFREQUENCY;
LONG res = ChangeDisplaySettings(&dm, CDS_FULLSCREEN);
if (res != DISP_CHANGE_SUCCESSFUL) {
// try again without forcing display frequency
dm.dmFields = DM_BITSPERPEL | DM_PELSWIDTH | DM_PELSHEIGHT;
res = ChangeDisplaySettings(&dm, CDS_FULLSCREEN);
}
bool ret = false;
switch(res) {
case DISP_CHANGE_SUCCESSFUL:
ChangedToFullScreen = true;
ret = true;
break;
case DISP_CHANGE_RESTART:
os::Printer::log("Switch to fullscreen: The computer must be restarted in order for the graphics mode to work.", ELL_ERROR);
break;
case DISP_CHANGE_BADFLAGS:
os::Printer::log("Switch to fullscreen: An invalid set of flags was passed in.", ELL_ERROR);
break;
case DISP_CHANGE_BADPARAM:
os::Printer::log("Switch to fullscreen: An invalid parameter was passed in. This can include an invalid flag or combination of flags.", ELL_ERROR);
break;
case DISP_CHANGE_FAILED:
os::Printer::log("Switch to fullscreen: The display driver failed the specified graphics mode.", ELL_ERROR);
break;
case DISP_CHANGE_BADMODE:
os::Printer::log("Switch to fullscreen: The graphics mode is not supported.", ELL_ERROR);
break;
default:
os::Printer::log("An unknown error occured while changing to fullscreen.", ELL_ERROR);
break;
}
_IRR_IMPLEMENT_MANAGED_MARSHALLING_BUGFIX;
return ret;
}
//! returns the win32 cursor control
CIrrDeviceWin32::CCursorControl* CIrrDeviceWin32::getWin32CursorControl() {
return Win32CursorControl;
}
//! \return Returns a pointer to a list with all video modes supported
//! by the gfx adapter.
video::IVideoModeList* CIrrDeviceWin32::getVideoModeList() {
if (!VideoModeList.getVideoModeCount()) {
// enumerate video modes.
DWORD i = 0;
DEVMODE mode;
memset(&mode, 0, sizeof(mode));
mode.dmSize = sizeof(mode);
while (EnumDisplaySettings(NULL, i, &mode)) {
VideoModeList.addMode(core::dimension2d<u32>(mode.dmPelsWidth, mode.dmPelsHeight),
mode.dmBitsPerPel);
++i;
}
if (EnumDisplaySettings(NULL, ENUM_CURRENT_SETTINGS, &mode))
VideoModeList.setDesktop(mode.dmBitsPerPel, core::dimension2d<u32>(mode.dmPelsWidth, mode.dmPelsHeight));
}
return &VideoModeList;
}
typedef BOOL (WINAPI *PGPI)(DWORD, DWORD, DWORD, DWORD, PDWORD);
// Needed for old windows apis
#ifndef PRODUCT_ULTIMATE
#define PRODUCT_ULTIMATE 0x00000001
#define PRODUCT_HOME_BASIC 0x00000002
#define PRODUCT_HOME_PREMIUM 0x00000003
#define PRODUCT_ENTERPRISE 0x00000004
#define PRODUCT_HOME_BASIC_N 0x00000005
#define PRODUCT_BUSINESS 0x00000006
#define PRODUCT_STARTER 0x0000000B
#define PRODUCT_BUSINESS_N 0x00000010
#define PRODUCT_HOME_PREMIUM_N 0x0000001A
#define PRODUCT_ENTERPRISE_N 0x0000001B
#define PRODUCT_ULTIMATE_N 0x0000001C
#define PRODUCT_STARTER_N 0x0000002F
#define PRODUCT_PROFESSIONAL 0x00000030
#define PRODUCT_PROFESSIONAL_N 0x00000031
#define PRODUCT_STARTER_E 0x00000042
#define PRODUCT_HOME_BASIC_E 0x00000043
#define PRODUCT_HOME_PREMIUM_E 0x00000044
#define PRODUCT_PROFESSIONAL_E 0x00000045
#define PRODUCT_ENTERPRISE_E 0x00000046
#define PRODUCT_ULTIMATE_E 0x00000047
#endif
void CIrrDeviceWin32::getWindowsVersion(core::stringc& out) {
OSVERSIONINFOEX osvi;
PGPI pGPI;
BOOL bOsVersionInfoEx;
ZeroMemory(&osvi, sizeof(OSVERSIONINFOEX));
osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFOEX);
bOsVersionInfoEx = GetVersionEx((OSVERSIONINFO*) &osvi);
if (!bOsVersionInfoEx) {
osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
if (! GetVersionEx((OSVERSIONINFO *) &osvi))
return;
}
switch (osvi.dwPlatformId) {
case VER_PLATFORM_WIN32_NT:
if (osvi.dwMajorVersion <= 4)
out.append("Microsoft Windows NT ");
else if (osvi.dwMajorVersion == 5 && osvi.dwMinorVersion == 0)
out.append("Microsoft Windows 2000 ");
else if (osvi.dwMajorVersion == 5 && osvi.dwMinorVersion == 1)
out.append("Microsoft Windows XP ");
else if (osvi.dwMajorVersion == 6 ) {
if (osvi.dwMinorVersion == 0) {
if (osvi.wProductType == VER_NT_WORKSTATION)
out.append("Microsoft Windows Vista ");
else
out.append("Microsoft Windows Server 2008 ");
} else if (osvi.dwMinorVersion == 1) {
if (osvi.wProductType == VER_NT_WORKSTATION)
out.append("Microsoft Windows 7 ");
else
out.append("Microsoft Windows Server 2008 R2 ");
}
}
if (bOsVersionInfoEx) {
if (osvi.dwMajorVersion == 6) {
DWORD dwType;
pGPI = (PGPI)GetProcAddress(GetModuleHandle(TEXT("kernel32.dll")), "GetProductInfo");
pGPI(osvi.dwMajorVersion, osvi.dwMinorVersion, 0, 0, &dwType);
switch (dwType) {
case PRODUCT_ULTIMATE:
case PRODUCT_ULTIMATE_E:
case PRODUCT_ULTIMATE_N:
out.append("Ultimate Edition ");
break;
case PRODUCT_PROFESSIONAL:
case PRODUCT_PROFESSIONAL_E:
case PRODUCT_PROFESSIONAL_N:
out.append("Professional Edition ");
break;
case PRODUCT_HOME_BASIC:
case PRODUCT_HOME_BASIC_E:
case PRODUCT_HOME_BASIC_N:
out.append("Home Basic Edition ");
break;
case PRODUCT_HOME_PREMIUM:
case PRODUCT_HOME_PREMIUM_E:
case PRODUCT_HOME_PREMIUM_N:
out.append("Home Premium Edition ");
break;
case PRODUCT_ENTERPRISE:
case PRODUCT_ENTERPRISE_E:
case PRODUCT_ENTERPRISE_N:
out.append("Enterprise Edition ");
break;
case PRODUCT_BUSINESS:
case PRODUCT_BUSINESS_N:
out.append("Business Edition ");
break;
case PRODUCT_STARTER:
case PRODUCT_STARTER_E:
case PRODUCT_STARTER_N:
out.append("Starter Edition ");
break;
}
}
#ifdef VER_SUITE_ENTERPRISE
else if (osvi.wProductType == VER_NT_WORKSTATION) {
#ifndef __BORLANDC__
if( osvi.wSuiteMask & VER_SUITE_PERSONAL )
out.append("Personal ");
else
out.append("Professional ");
#endif
} else if (osvi.wProductType == VER_NT_SERVER) {
if( osvi.wSuiteMask & VER_SUITE_DATACENTER )
out.append("DataCenter Server ");
else if( osvi.wSuiteMask & VER_SUITE_ENTERPRISE )
out.append("Advanced Server ");
else
out.append("Server ");
}
#endif
} else {
HKEY hKey;
char szProductType[80];
DWORD dwBufLen;
RegOpenKeyEx( HKEY_LOCAL_MACHINE,
__TEXT("SYSTEM\\CurrentControlSet\\Control\\ProductOptions"),
0, KEY_QUERY_VALUE, &hKey );
RegQueryValueEx( hKey, __TEXT("ProductType"), NULL, NULL,
(LPBYTE) szProductType, &dwBufLen);
RegCloseKey( hKey );
if (_strcmpi( "WINNT", szProductType) == 0 )
out.append("Professional ");
if (_strcmpi( "LANMANNT", szProductType) == 0)
out.append("Server ");
if (_strcmpi( "SERVERNT", szProductType) == 0)
out.append("Advanced Server ");
}
// Display version, service pack (if any), and build number.
char tmp[255];
if (osvi.dwMajorVersion <= 4 ) {
sprintf(tmp, "version %ld.%ld %s (Build %ld)",
osvi.dwMajorVersion,
osvi.dwMinorVersion,
osvi.szCSDVersion,
osvi.dwBuildNumber & 0xFFFF);
} else {
sprintf(tmp, "%s (Build %ld)", osvi.szCSDVersion,
osvi.dwBuildNumber & 0xFFFF);
}
out.append(tmp);
break;
case VER_PLATFORM_WIN32_WINDOWS:
IsNonNTWindows = true;
if (osvi.dwMajorVersion == 4 && osvi.dwMinorVersion == 0) {
out.append("Microsoft Windows 95 ");
if ( osvi.szCSDVersion[1] == 'C' || osvi.szCSDVersion[1] == 'B' )
out.append("OSR2 " );
}
if (osvi.dwMajorVersion == 4 && osvi.dwMinorVersion == 10) {
out.append("Microsoft Windows 98 ");
if ( osvi.szCSDVersion[1] == 'A' )
out.append( "SE " );
}
if (osvi.dwMajorVersion == 4 && osvi.dwMinorVersion == 90)
out.append("Microsoft Windows Me ");
break;
case VER_PLATFORM_WIN32s:
IsNonNTWindows = true;
out.append("Microsoft Win32s ");
break;
}
}
//! Notifies the device, that it has been resized
void CIrrDeviceWin32::OnResized() {
Resized = true;
}
//! Sets if the window should be resizable in windowed mode.
void CIrrDeviceWin32::setResizable(bool resize) {
if (ExternalWindow || !getVideoDriver() || CreationParams.Fullscreen)
return;
LONG_PTR style = WS_POPUP;
if (!resize)
style = WS_SYSMENU | WS_BORDER | WS_CAPTION | WS_CLIPCHILDREN | WS_CLIPSIBLINGS;
else
style = WS_THICKFRAME | WS_SYSMENU | WS_CAPTION | WS_CLIPCHILDREN | WS_CLIPSIBLINGS | WS_MINIMIZEBOX | WS_MAXIMIZEBOX;
if (!SetWindowLongPtr(HWnd, GWL_STYLE, style))
os::Printer::log("Could not change window style.");
RECT clientSize;
clientSize.top = 0;
clientSize.left = 0;
clientSize.right = getVideoDriver()->getScreenSize().Width;
clientSize.bottom = getVideoDriver()->getScreenSize().Height;
AdjustWindowRect(&clientSize, style, FALSE);
const s32 realWidth = clientSize.right - clientSize.left;
const s32 realHeight = clientSize.bottom - clientSize.top;
const s32 windowLeft = (GetSystemMetrics(SM_CXSCREEN) - realWidth) / 2;
const s32 windowTop = (GetSystemMetrics(SM_CYSCREEN) - realHeight) / 2;
SetWindowPos(HWnd, HWND_TOP, windowLeft, windowTop, realWidth, realHeight,
SWP_FRAMECHANGED | SWP_NOMOVE | SWP_SHOWWINDOW);
static_cast<CCursorControl*>(CursorControl)->updateBorderSize(CreationParams.Fullscreen, resize);
}
//! Minimizes the window.
void CIrrDeviceWin32::minimizeWindow() {
WINDOWPLACEMENT wndpl;
wndpl.length = sizeof(WINDOWPLACEMENT);
GetWindowPlacement(HWnd, &wndpl);
wndpl.showCmd = SW_SHOWMINNOACTIVE;
SetWindowPlacement(HWnd, &wndpl);
}
//! Maximizes the window.
void CIrrDeviceWin32::maximizeWindow() {
WINDOWPLACEMENT wndpl;
wndpl.length = sizeof(WINDOWPLACEMENT);
GetWindowPlacement(HWnd, &wndpl);
wndpl.showCmd = SW_SHOWMAXIMIZED;
SetWindowPlacement(HWnd, &wndpl);
}
//! Restores the window to its original size.
void CIrrDeviceWin32::restoreWindow() {
WINDOWPLACEMENT wndpl;
wndpl.length = sizeof(WINDOWPLACEMENT);
GetWindowPlacement(HWnd, &wndpl);
wndpl.showCmd = SW_SHOWNORMAL;
SetWindowPlacement(HWnd, &wndpl);
}
bool CIrrDeviceWin32::activateJoysticks(core::array<SJoystickInfo> & joystickInfo) {
#if defined _IRR_COMPILE_WITH_JOYSTICK_EVENTS_
joystickInfo.clear();
ActiveJoysticks.clear();
const u32 numberOfJoysticks = ::joyGetNumDevs();
JOYINFOEX info;
info.dwSize = sizeof(info);
info.dwFlags = JOY_RETURNALL;
JoystickInfo activeJoystick;
SJoystickInfo returnInfo;
joystickInfo.reallocate(numberOfJoysticks);
ActiveJoysticks.reallocate(numberOfJoysticks);
u32 joystick = 0;
for(; joystick < numberOfJoysticks; ++joystick) {
if(JOYERR_NOERROR == joyGetPosEx(joystick, &info)
&&
JOYERR_NOERROR == joyGetDevCaps(joystick,
&activeJoystick.Caps,
sizeof(activeJoystick.Caps))) {
activeJoystick.Index = joystick;
ActiveJoysticks.push_back(activeJoystick);
returnInfo.Joystick = (u8)joystick;
returnInfo.Axes = activeJoystick.Caps.wNumAxes;
returnInfo.Buttons = activeJoystick.Caps.wNumButtons;
returnInfo.Name = activeJoystick.Caps.szPname;
returnInfo.PovHat = ((activeJoystick.Caps.wCaps & JOYCAPS_HASPOV) == JOYCAPS_HASPOV)
? SJoystickInfo::POV_HAT_PRESENT : SJoystickInfo::POV_HAT_ABSENT;
joystickInfo.push_back(returnInfo);
}
}
for(joystick = 0; joystick < joystickInfo.size(); ++joystick) {
char logString[256];
(void)sprintf(logString, "Found joystick %d, %d axes, %d buttons '%s'",
joystick, joystickInfo[joystick].Axes,
joystickInfo[joystick].Buttons, joystickInfo[joystick].Name.c_str());
os::Printer::log(logString, ELL_INFORMATION);
}
return true;
#else
return false;
#endif // _IRR_COMPILE_WITH_JOYSTICK_EVENTS_
}
void CIrrDeviceWin32::pollJoysticks() {
#if defined _IRR_COMPILE_WITH_JOYSTICK_EVENTS_
if(0 == ActiveJoysticks.size())
return;
u32 joystick;
JOYINFOEX info;
for(joystick = 0; joystick < ActiveJoysticks.size(); ++joystick) {
// needs to be reset for each joystick
// request ALL values and POV as continuous if possible
info.dwSize = sizeof(info);
info.dwFlags = JOY_RETURNALL | JOY_RETURNPOVCTS;
const JOYCAPS & caps = ActiveJoysticks[joystick].Caps;
// if no POV is available don't ask for POV values
if (!(caps.wCaps & JOYCAPS_HASPOV))
info.dwFlags &= ~(JOY_RETURNPOV | JOY_RETURNPOVCTS);
if(JOYERR_NOERROR == joyGetPosEx(ActiveJoysticks[joystick].Index, &info)) {
SEvent event;
event.EventType = irr::EET_JOYSTICK_INPUT_EVENT;
event.JoystickEvent.Joystick = (u8)joystick;
event.JoystickEvent.POV = (u16)info.dwPOV;
// set to undefined if no POV value was returned or the value
// is out of range
if (!(info.dwFlags & JOY_RETURNPOV) || (event.JoystickEvent.POV > 35900))
event.JoystickEvent.POV = 65535;
for(int axis = 0; axis < SEvent::SJoystickEvent::NUMBER_OF_AXES; ++axis)
event.JoystickEvent.Axis[axis] = 0;
switch(caps.wNumAxes) {
default:
case 6:
event.JoystickEvent.Axis[SEvent::SJoystickEvent::AXIS_V] =
(s16)((65535 * (info.dwVpos - caps.wVmin)) / (caps.wVmax - caps.wVmin) - 32768);
case 5:
event.JoystickEvent.Axis[SEvent::SJoystickEvent::AXIS_U] =
(s16)((65535 * (info.dwUpos - caps.wUmin)) / (caps.wUmax - caps.wUmin) - 32768);
case 4:
event.JoystickEvent.Axis[SEvent::SJoystickEvent::AXIS_R] =
(s16)((65535 * (info.dwRpos - caps.wRmin)) / (caps.wRmax - caps.wRmin) - 32768);
case 3:
event.JoystickEvent.Axis[SEvent::SJoystickEvent::AXIS_Z] =
(s16)((65535 * (info.dwZpos - caps.wZmin)) / (caps.wZmax - caps.wZmin) - 32768);
case 2:
event.JoystickEvent.Axis[SEvent::SJoystickEvent::AXIS_Y] =
(s16)((65535 * (info.dwYpos - caps.wYmin)) / (caps.wYmax - caps.wYmin) - 32768);
case 1:
event.JoystickEvent.Axis[SEvent::SJoystickEvent::AXIS_X] =
(s16)((65535 * (info.dwXpos - caps.wXmin)) / (caps.wXmax - caps.wXmin) - 32768);
}
event.JoystickEvent.ButtonStates = info.dwButtons;
(void)postEventFromUser(event);
}
}
#endif // _IRR_COMPILE_WITH_JOYSTICK_EVENTS_
}
//! Set the current Gamma Value for the Display
bool CIrrDeviceWin32::setGammaRamp( f32 red, f32 green, f32 blue, f32 brightness, f32 contrast ) {
bool r;
u16 ramp[3][256];
calculateGammaRamp( ramp[0], red, brightness, contrast );
calculateGammaRamp( ramp[1], green, brightness, contrast );
calculateGammaRamp( ramp[2], blue, brightness, contrast );
HDC dc = GetDC(0);
r = SetDeviceGammaRamp ( dc, ramp ) == TRUE;
ReleaseDC(HWnd, dc);
return r;
}
//! Get the current Gamma Value for the Display
bool CIrrDeviceWin32::getGammaRamp( f32 &red, f32 &green, f32 &blue, f32 &brightness, f32 &contrast ) {
bool r;
u16 ramp[3][256];
HDC dc = GetDC(0);
r = GetDeviceGammaRamp ( dc, ramp ) == TRUE;
ReleaseDC(HWnd, dc);
if ( r ) {
calculateGammaFromRamp(red, ramp[0]);
calculateGammaFromRamp(green, ramp[1]);
calculateGammaFromRamp(blue, ramp[2]);
}
brightness = 0.f;
contrast = 0.f;
return r;
}
//! Remove all messages pending in the system message loop
void CIrrDeviceWin32::clearSystemMessages() {
MSG msg;
while (PeekMessage(&msg, NULL, WM_KEYFIRST, WM_KEYLAST, PM_REMOVE))
{}
while (PeekMessage(&msg, NULL, WM_MOUSEFIRST, WM_MOUSELAST, PM_REMOVE))
{}
}
// shows last error in a messagebox to help internal debugging.
void CIrrDeviceWin32::ReportLastWinApiError() {
// (based on code from ovidiucucu from http://www.codeguru.com/forum/showthread.php?t=318721)
LPCTSTR pszCaption = __TEXT("Windows SDK Error Report");
DWORD dwError = GetLastError();
if(NOERROR == dwError) {
MessageBox(NULL, __TEXT("No error"), pszCaption, MB_OK);
} else {
const DWORD dwFormatControl = FORMAT_MESSAGE_ALLOCATE_BUFFER |
FORMAT_MESSAGE_IGNORE_INSERTS |
FORMAT_MESSAGE_FROM_SYSTEM;
LPVOID pTextBuffer = NULL;
DWORD dwCount = FormatMessage(dwFormatControl,
NULL,
dwError,
0,
(LPTSTR) &pTextBuffer,
0,
NULL);
if(0 != dwCount) {
MessageBox(NULL, (LPCTSTR)pTextBuffer, pszCaption, MB_OK | MB_ICONERROR);
LocalFree(pTextBuffer);
} else {
MessageBox(NULL, __TEXT("Unknown error"), pszCaption, MB_OK | MB_ICONERROR);
}
}
}
} // end namespace
#endif // _IRR_COMPILE_WITH_WINDOWS_DEVICE_
#endif // _WIN32
// Copyright (C) 2002-2010 Nikolaus Gebhardt
// This file is part of the "Irrlicht Engine".
// For conditions of distribution and use, see copyright notice in irrlicht.h
#ifdef _WIN32
#ifndef __C_IRR_DEVICE_WIN32_H_INCLUDED__
#define __C_IRR_DEVICE_WIN32_H_INCLUDED__
#include "IrrCompileConfig.h"
#ifdef _IRR_COMPILE_WITH_WINDOWS_DEVICE_
#include "CIrrDeviceStub.h"
#include "IrrlichtDevice.h"
#include "IImagePresenter.h"
#define WIN32_LEAN_AND_MEAN
#if !defined(_IRR_XBOX_PLATFORM_)
#include <windows.h>
#include <mmsystem.h> // For JOYCAPS
#include <Windowsx.h>
#endif
namespace irr
{
class CIrrDeviceWin32 : public CIrrDeviceStub, video::IImagePresenter
{
public:
//! constructor
CIrrDeviceWin32(const SIrrlichtCreationParameters& params);
//! destructor
virtual ~CIrrDeviceWin32();
//! runs the device. Returns false if device wants to be deleted
virtual bool run();
//! Cause the device to temporarily pause execution and let other processes to run
// This should bring down processor usage without major performance loss for Irrlicht
virtual void yield();
//! Pause execution and let other processes to run for a specified amount of time.
virtual void sleep(u32 timeMs, bool pauseTimer);
//! sets the caption of the window
virtual void setWindowCaption(const wchar_t* text);
//! returns if window is active. if not, nothing need to be drawn
virtual bool isWindowActive() const;
//! returns if window has focus
virtual bool isWindowFocused() const;
//! returns if window is minimized
virtual bool isWindowMinimized() const;
//! presents a surface in the client area
virtual bool present(video::IImage* surface, void* windowId=0, core::rect<s32>* src=0);
//! notifies the device that it should close itself
virtual void closeDevice();
//! \return Returns a pointer to a list with all video modes
//! supported by the gfx adapter.
video::IVideoModeList* getVideoModeList();
//! Notifies the device, that it has been resized
void OnResized();
//! Sets if the window should be resizable in windowed mode.
virtual void setResizable(bool resize=false);
//! Minimizes the window.
virtual void minimizeWindow();
//! Maximizes the window.
virtual void maximizeWindow();
//! Restores the window size.
virtual void restoreWindow();
//! Activate any joysticks, and generate events for them.
virtual bool activateJoysticks(core::array<SJoystickInfo> & joystickInfo);
//! Set the current Gamma Value for the Display
virtual bool setGammaRamp( f32 red, f32 green, f32 blue, f32 brightness, f32 contrast );
//! Get the current Gamma Value for the Display
virtual bool getGammaRamp( f32 &red, f32 &green, f32 &blue, f32 &brightness, f32 &contrast );
//! Remove all messages pending in the system message loop
virtual void clearSystemMessages();
//! Get the device type
virtual E_DEVICE_TYPE getType() const
{
return EIDT_WIN32;
}
//! Compares to the last call of this function to return double and triple clicks.
//! \return Returns only 1,2 or 3. A 4th click will start with 1 again.
virtual u32 checkSuccessiveClicks(s32 mouseX, s32 mouseY, EMOUSE_INPUT_EVENT inputEvent )
{
// we just have to make it public
return CIrrDeviceStub::checkSuccessiveClicks(mouseX, mouseY, inputEvent );
}
//! switchs to fullscreen
bool switchToFullScreen(bool reset=false);
//! Check for and show last Windows API error to help internal debugging.
//! Does call GetLastError and on errors formats the errortext and displays it in a messagebox.
static void ReportLastWinApiError();
//! Implementation of the win32 cursor control
class CCursorControl : public gui::ICursorControl
{
public:
CCursorControl(const core::dimension2d<u32>& wsize, HWND hwnd, bool fullscreen)
: WindowSize(wsize), InvWindowSize(0.0f, 0.0f),
HWnd(hwnd), BorderX(0), BorderY(0),
UseReferenceRect(false), IsVisible(true)
{
if (WindowSize.Width!=0)
InvWindowSize.Width = 1.0f / WindowSize.Width;
if (WindowSize.Height!=0)
InvWindowSize.Height = 1.0f / WindowSize.Height;
updateBorderSize(fullscreen, false);
}
//! Changes the visible state of the mouse cursor.
virtual void setVisible(bool visible)
{
CURSORINFO info;
info.cbSize = sizeof(CURSORINFO);
BOOL gotCursorInfo = GetCursorInfo(&info);
while ( gotCursorInfo )
{
if ( (visible && info.flags == CURSOR_SHOWING) // visible
|| (!visible && info.flags == 0 ) ) // hidden
{
break;
}
int showResult = ShowCursor(visible); // this only increases an internal display counter in windows, so it might have to be called some more
if ( showResult < 0 )
{
break;
}
info.cbSize = sizeof(CURSORINFO); // yes, it really must be set each time
gotCursorInfo = GetCursorInfo(&info);
}
IsVisible = visible;
}
//! Returns if the cursor is currently visible.
virtual bool isVisible() const
{
_IRR_IMPLEMENT_MANAGED_MARSHALLING_BUGFIX;
return IsVisible;
}
//! Sets the new position of the cursor.
virtual void setPosition(const core::position2d<f32> &pos)
{
setPosition(pos.X, pos.Y);
}
//! Sets the new position of the cursor.
virtual void setPosition(f32 x, f32 y)
{
if (!UseReferenceRect)
setPosition(core::round32(x*WindowSize.Width), core::round32(y*WindowSize.Height));
else
setPosition(core::round32(x*ReferenceRect.getWidth()), core::round32(y*ReferenceRect.getHeight()));
}
//! Sets the new position of the cursor.
virtual void setPosition(const core::position2d<s32> &pos)
{
setPosition(pos.X, pos.Y);
}
//! Sets the new position of the cursor.
virtual void setPosition(s32 x, s32 y)
{
if (UseReferenceRect)
{
SetCursorPos(ReferenceRect.UpperLeftCorner.X + x,
ReferenceRect.UpperLeftCorner.Y + y);
}
else
{
RECT rect;
if (GetWindowRect(HWnd, &rect))
SetCursorPos(x + rect.left + BorderX, y + rect.top + BorderY);
}
CursorPos.X = x;
CursorPos.Y = y;
}
//! Returns the current position of the mouse cursor.
virtual const core::position2d<s32>& getPosition()
{
updateInternalCursorPosition();
return CursorPos;
}
//! Returns the current position of the mouse cursor.
virtual core::position2d<f32> getRelativePosition()
{
updateInternalCursorPosition();
if (!UseReferenceRect)
{
return core::position2d<f32>(CursorPos.X * InvWindowSize.Width,
CursorPos.Y * InvWindowSize.Height);
}
return core::position2d<f32>(CursorPos.X / (f32)ReferenceRect.getWidth(),
CursorPos.Y / (f32)ReferenceRect.getHeight());
}
//! Sets an absolute reference rect for calculating the cursor position.
virtual void setReferenceRect(core::rect<s32>* rect=0)
{
if (rect)
{
ReferenceRect = *rect;
UseReferenceRect = true;
// prevent division through zero and uneven sizes
if (!ReferenceRect.getHeight() || ReferenceRect.getHeight()%2)
ReferenceRect.LowerRightCorner.Y += 1;
if (!ReferenceRect.getWidth() || ReferenceRect.getWidth()%2)
ReferenceRect.LowerRightCorner.X += 1;
}
else
UseReferenceRect = false;
}
/** Used to notify the cursor that the window was resized. */
virtual void OnResize(const core::dimension2d<u32>& size)
{
WindowSize = size;
if (size.Width!=0)
InvWindowSize.Width = 1.0f / size.Width;
else
InvWindowSize.Width = 0.f;
if (size.Height!=0)
InvWindowSize.Height = 1.0f / size.Height;
else
InvWindowSize.Height = 0.f;
}
/** Used to notify the cursor that the window resizable settings changed. */
void updateBorderSize(bool fullscreen, bool resizable)
{
if (!fullscreen)
{
if (resizable)
{
BorderX = GetSystemMetrics(SM_CXSIZEFRAME);
BorderY = GetSystemMetrics(SM_CYCAPTION) + GetSystemMetrics(SM_CYSIZEFRAME);
}
else
{
BorderX = GetSystemMetrics(SM_CXDLGFRAME);
BorderY = GetSystemMetrics(SM_CYCAPTION) + GetSystemMetrics(SM_CYDLGFRAME);
}
}
else
{
BorderX = BorderY = 0;
}
}
private:
//! Updates the internal cursor position
void updateInternalCursorPosition()
{
POINT p;
if (!GetCursorPos(&p))
{
DWORD xy = GetMessagePos();
p.x = GET_X_LPARAM(xy);
p.y = GET_Y_LPARAM(xy);
}
if (UseReferenceRect)
{
CursorPos.X = p.x - ReferenceRect.UpperLeftCorner.X;
CursorPos.Y = p.y - ReferenceRect.UpperLeftCorner.Y;
}
else
{
RECT rect;
if (GetWindowRect(HWnd, &rect))
{
CursorPos.X = p.x-rect.left-BorderX;
CursorPos.Y = p.y-rect.top-BorderY;
}
else
{
// window seems not to be existent, so set cursor to
// a negative value
CursorPos.X = -1;
CursorPos.Y = -1;
}
}
}
core::position2d<s32> CursorPos;
core::dimension2d<u32> WindowSize;
core::dimension2d<f32> InvWindowSize;
HWND HWnd;
s32 BorderX, BorderY;
core::rect<s32> ReferenceRect;
bool UseReferenceRect;
bool IsVisible;
};
//! returns the win32 cursor control
CCursorControl* getWin32CursorControl();
private:
//! create the driver
void createDriver();
void getWindowsVersion(core::stringc& version);
void resizeIfNecessary();
void pollJoysticks();
HWND HWnd;
bool ChangedToFullScreen;
bool IsNonNTWindows;
bool Resized;
bool ExternalWindow;
CCursorControl* Win32CursorControl;
#if defined(_IRR_COMPILE_WITH_JOYSTICK_EVENTS_)
struct JoystickInfo
{
u32 Index;
JOYCAPS Caps;
};
core::array<JoystickInfo> ActiveJoysticks;
#endif
};
} // end namespace irr
#endif // _IRR_COMPILE_WITH_WINDOWS_DEVICE_
#endif // __C_IRR_DEVICE_WIN32_H_INCLUDED__
#endif //_WIN32
// Copyright (C) 2002-2010 Nikolaus Gebhardt
// This file is part of the "Irrlicht Engine".
// For conditions of distribution and use, see copyright notice in irrlicht.h
#include "COSOperator.h"
#ifdef _IRR_WINDOWS_API_
#ifndef _IRR_XBOX_PLATFORM_
#include <windows.h>
#endif
#else
#include <string.h>
#include <unistd.h>
#ifndef _IRR_SOLARIS_PLATFORM_
#include <sys/types.h>
#include <sys/sysctl.h>
#endif
#endif
#if defined(_IRR_COMPILE_WITH_X11_DEVICE_)
#include "CIrrDeviceLinux.h"
#endif
#ifdef _IRR_COMPILE_WITH_OSX_DEVICE_
#include "MacOSX/OSXClipboard.h"
#endif
namespace irr
{
#if defined(_IRR_COMPILE_WITH_X11_DEVICE_)
// constructor linux
COSOperator::COSOperator(const c8* osversion, CIrrDeviceLinux* device)
: IrrDeviceLinux(device)
{
}
#endif
// constructor
COSOperator::COSOperator(const c8* osVersion) : OperatingSystem(osVersion)
{
#ifdef _DEBUG
setDebugName("COSOperator");
#endif
}
//! returns the current operating system version as string.
const wchar_t* COSOperator::getOperationSystemVersion() const
{
return OperatingSystem.c_str();
}
//! copies text to the clipboard
void COSOperator::copyToClipboard(const c8* text) const
{
if (strlen(text)==0)
return;
// Windows version
#if defined(_IRR_XBOX_PLATFORM_)
#elif defined(_IRR_WINDOWS_API_)
if (!OpenClipboard(NULL) || text == 0)
return;
EmptyClipboard();
HGLOBAL clipbuffer;
wchar_t* buffer;
clipbuffer = GlobalAlloc(GMEM_DDESHARE, (wcslen((wchar_t*)text)+1) * sizeof(wchar_t*));
buffer = (wchar_t*)GlobalLock(clipbuffer);
wcscpy(buffer, (wchar_t*)text);
GlobalUnlock(clipbuffer);
SetClipboardData(CF_UNICODETEXT, clipbuffer);
CloseClipboard();
// MacOSX version
#elif defined(_IRR_COMPILE_WITH_OSX_DEVICE_)
OSXCopyToClipboard(text);
#elif defined(_IRR_COMPILE_WITH_X11_DEVICE_)
if ( IrrDeviceLinux )
IrrDeviceLinux->copyToClipboard(text);
#else
#endif
}
//! gets text from the clipboard
//! \return Returns 0 if no string is in there.
const c8* COSOperator::getTextFromClipboard() const
{
#if defined(_IRR_XBOX_PLATFORM_)
return 0;
#elif defined(_IRR_WINDOWS_API_)
if (!OpenClipboard(NULL))
return 0;
char * buffer = 0;
HANDLE hData = GetClipboardData(CF_UNICODETEXT);
buffer = (char*)GlobalLock( hData );
GlobalUnlock( hData );
CloseClipboard();
return buffer;
#elif defined(_IRR_COMPILE_WITH_OSX_DEVICE_)
return (OSXCopyFromClipboard());
#elif defined(_IRR_COMPILE_WITH_X11_DEVICE_)
if ( IrrDeviceLinux )
return IrrDeviceLinux->getTextFromClipboard();
return 0;
#else
return 0;
#endif
}
bool COSOperator::getProcessorSpeedMHz(u32* MHz) const
{
#if defined(_IRR_WINDOWS_API_) && !defined(_WIN32_WCE ) && !defined (_IRR_XBOX_PLATFORM_)
LONG Error;
HKEY Key;
Error = RegOpenKeyEx(HKEY_LOCAL_MACHINE,
__TEXT("HARDWARE\\DESCRIPTION\\System\\CentralProcessor\\0"),
0, KEY_READ, &Key);
if(Error != ERROR_SUCCESS)
return false;
DWORD Speed = 0;
DWORD Size = sizeof(Speed);
Error = RegQueryValueEx(Key, __TEXT("~MHz"), NULL, NULL, (LPBYTE)&Speed, &Size);
RegCloseKey(Key);
if (Error != ERROR_SUCCESS)
return false;
else if (MHz)
*MHz = Speed;
_IRR_IMPLEMENT_MANAGED_MARSHALLING_BUGFIX;
return true;
#elif defined(_IRR_OSX_PLATFORM_)
struct clockinfo CpuClock;
size_t Size = sizeof(clockinfo);
if (!sysctlbyname("kern.clockrate", &CpuClock, &Size, NULL, 0))
return false;
else if (MHz)
*MHz = CpuClock.hz;
return true;
#else
// could probably be read from "/proc/cpuinfo" or "/proc/cpufreq"
return false;
#endif
}
bool COSOperator::getSystemMemory(u32* Total, u32* Avail) const
{
#if defined(_IRR_WINDOWS_API_) && !defined (_IRR_XBOX_PLATFORM_)
MEMORYSTATUS MemoryStatus;
MemoryStatus.dwLength = sizeof(MEMORYSTATUS);
// cannot fail
GlobalMemoryStatus(&MemoryStatus);
if (Total)
*Total = (u32)(MemoryStatus.dwTotalPhys>>10);
if (Avail)
*Avail = (u32)(MemoryStatus.dwAvailPhys>>10);
_IRR_IMPLEMENT_MANAGED_MARSHALLING_BUGFIX;
return true;
#elif defined(_IRR_POSIX_API_) && !defined(__FreeBSD__)
#if defined(_SC_PHYS_PAGES) && defined(_SC_AVPHYS_PAGES)
long ps = sysconf(_SC_PAGESIZE);
long pp = sysconf(_SC_PHYS_PAGES);
long ap = sysconf(_SC_AVPHYS_PAGES);
if ((ps==-1)||(pp==-1)||(ap==-1))
return false;
if (Total)
*Total = (u32)((ps*(long long)pp)>>10);
if (Avail)
*Avail = (u32)((ps*(long long)ap)>>10);
return true;
#else
// TODO: implement for non-availablity of symbols/features
return false;
#endif
#else
// TODO: implement for OSX
return false;
#endif
}
} // end namespace
// Copyright (C) 2002-2010 Nikolaus Gebhardt
// This file is part of the "Irrlicht Engine".
// For conditions of distribution and use, see copyright notice in irrlicht.h
#ifndef __C_OS_OPERATOR_H_INCLUDED__
#define __C_OS_OPERATOR_H_INCLUDED__
#include "IOSOperator.h"
#include "irrString.h"
#include "IrrCompileConfig.h"
namespace irr
{
class CIrrDeviceLinux;
//! The Operating system operator provides operation system specific methods and informations.
class COSOperator : public IOSOperator
{
public:
// constructor
#if defined(_IRR_COMPILE_WITH_X11_DEVICE_)
COSOperator(const c8* osversion, CIrrDeviceLinux* device);
#endif
COSOperator(const c8* osversion);
//! returns the current operation system version as string.
virtual const wchar_t* getOperationSystemVersion() const;
//! copies text to the clipboard
virtual void copyToClipboard(const c8* text) const;
//! gets text from the clipboard
//! \return Returns 0 if no string is in there.
virtual const c8* getTextFromClipboard() const;
//! gets the processor speed in megahertz
//! \param Mhz:
//! \return Returns true if successful, false if not
virtual bool getProcessorSpeedMHz(u32* MHz) const;
//! gets the total and available system RAM in kB
//! \param Total: will contain the total system memory
//! \param Avail: will contain the available memory
//! \return Returns true if successful, false if not
virtual bool getSystemMemory(u32* Total, u32* Avail) const;
private:
core::stringw OperatingSystem;
#if defined(_IRR_COMPILE_WITH_X11_DEVICE_)
CIrrDeviceLinux * IrrDeviceLinux;
#endif
};
} // end namespace
#endif
// Copyright (C) 2002-2010 Nikolaus Gebhardt
// This file is part of the "Irrlicht Engine".
// For conditions of distribution and use, see copyright notice in irrlicht.h
#ifndef __C_IRR_C_TIMER_H_INCLUDED__
#define __C_IRR_C_TIMER_H_INCLUDED__
#include "ITimer.h"
#include "os.h"
namespace irr
{
//! Device independent implementation of the timer
class CTimer : public ITimer
{
public:
CTimer()
{
os::Timer::initTimer();
}
//! Returns current real time in milliseconds of the system.
/** This value does not start with 0 when the application starts.
For example in one implementation the value returned could be the
amount of milliseconds which have elapsed since the system was started. */
virtual u32 getRealTime() const
{
return os::Timer::getRealTime();
}
//! Returns current virtual time in milliseconds.
/** This value starts with 0 and can be manipulated using setTime(), stopTimer(),
startTimer(), etc. This value depends on the set speed of the timer if the timer
is stopped, etc. If you need the system time, use getRealTime() */
virtual u32 getTime() const
{
return os::Timer::getTime();
}
//! sets current virtual time
virtual void setTime(u32 time)
{
os::Timer::setTime(time);
}
//! Stops the game timer.
/** The timer is reference counted, which means everything which calls
stopTimer() will also have to call startTimer(), otherwise the timer may not start/stop
corretly again. */
virtual void stop()
{
os::Timer::stopTimer();
}
//! Starts the game timer.
/** The timer is reference counted, which means everything which calls
stopTimer() will also have to call startTimer(), otherwise the timer may not start/stop
corretly again. */
virtual void start()
{
os::Timer::startTimer();
}
//! Sets the speed of the timer
/** The speed is the factor with which the time is running faster or slower then the
real system time. */
virtual void setSpeed(f32 speed = 1.0f)
{
os::Timer::setSpeed(speed);
}
//! Returns current speed of the timer
/** The speed is the factor with which the time is running faster or slower then the
real system time. */
virtual f32 getSpeed() const
{
return os::Timer::getSpeed();
}
//! Returns if game timer is currently stopped
virtual bool isStopped() const
{
bool ret = os::Timer::isStopped();
_IRR_IMPLEMENT_MANAGED_MARSHALLING_BUGFIX;
return ret;
}
//! Advances the virtual time
/** Makes the virtual timer update the time value based on the real time. This is
called automaticly when calling IrrlichtDevice::run(), but you can call it manually
if you don't use this method. */
virtual void tick()
{
os::Timer::tick();
}
};
} // end namespace
#endif
// Copyright (C) 2002-2010 Nikolaus Gebhardt
// This file is part of the "Irrlicht Engine".
// For conditions of distribution and use, see copyright notice in irrlicht.h
#ifndef __IRR_C_VIDEO_MODE_LIST_H_INCLUDED__
#define __IRR_C_VIDEO_MODE_LIST_H_INCLUDED__
#include "IVideoModeList.h"
#include "dimension2d.h"
#include "irrArray.h"
namespace irr
{
namespace video
{
class CVideoModeList : public IVideoModeList
{
public:
//! constructor
CVideoModeList();
//! Gets amount of video modes in the list.
virtual s32 getVideoModeCount() const;
//! Returns the screen size of a video mode in pixels.
virtual core::dimension2d<u32> getVideoModeResolution(s32 modeNumber) const;
//! Returns the screen size of an optimal video mode in pixels.
virtual core::dimension2d<u32> getVideoModeResolution(const core::dimension2d<u32>& minSize, const core::dimension2d<u32>& maxSize) const;
//! Returns the pixel depth of a video mode in bits.
virtual s32 getVideoModeDepth(s32 modeNumber) const;
//! Returns current desktop screen resolution.
virtual const core::dimension2d<u32>& getDesktopResolution() const;
//! Returns the pixel depth of a video mode in bits.
virtual s32 getDesktopDepth() const;
//! adds a new mode to the list
void addMode(const core::dimension2d<u32>& size, s32 depth);
void setDesktop(s32 desktopDepth, const core::dimension2d<u32>& desktopSize);
private:
struct SVideoMode
{
core::dimension2d<u32> size;
s32 depth;
bool operator==(const SVideoMode& other) const
{
return size == other.size && depth == other.depth;
}
bool operator <(const SVideoMode& other) const
{
return (size.Width < other.size.Width ||
(size.Width == other.size.Width &&
size.Height < other.size.Height) ||
(size.Width == other.size.Width &&
size.Height == other.size.Height &&
depth < other.depth));
}
};
core::array<SVideoMode> VideoModes;
SVideoMode Desktop;
};
} // end namespace video
} // end namespace irr
#endif
// Copyright (C) 2002-2010 Nikolaus Gebhardt
// This file is part of the "Irrlicht Engine".
// For conditions of distribution and use, see copyright notice in irrlicht.h
#ifndef __I_IMAGE_PRESENTER_H_INCLUDED__
#define __I_IMAGE_PRESENTER_H_INCLUDED__
#include "IImage.h"
namespace irr
{
namespace video
{
/*!
Interface for a class which is able to present an IImage
an the Screen. Usually only implemented by an IrrDevice for
presenting Software Device Rendered images.
This class should be used only internally.
*/
class IImagePresenter
{
public:
virtual ~IImagePresenter() {};
//! presents a surface in the client area
virtual bool present(video::IImage* surface, void* windowId=0, core::rect<s32>* src=0 ) = 0;
};
} // end namespace video
} // end namespace irr
#endif
......@@ -11,7 +11,7 @@ namespace ygo {
struct CardData {
unsigned int code;
unsigned int alias;
unsigned int setcode;
unsigned long long setcode;
unsigned int type;
unsigned int level;
unsigned int attribute;
......@@ -22,7 +22,7 @@ struct CardData {
struct CardDataC {
unsigned int code;
unsigned int alias;
unsigned int setcode;
unsigned long long setcode;
unsigned int type;
unsigned int level;
unsigned int attribute;
......
......@@ -27,7 +27,7 @@ bool DataManager::LoadDB(const char* file) {
cd.code = sqlite3_column_int(pStmt, 0);
cd.ot = sqlite3_column_int(pStmt, 1);
cd.alias = sqlite3_column_int(pStmt, 2);
cd.setcode = sqlite3_column_int(pStmt, 3);
cd.setcode = sqlite3_column_int64(pStmt, 3);
cd.type = sqlite3_column_int(pStmt, 4);
cd.attack = sqlite3_column_int(pStmt, 5);
cd.defence = sqlite3_column_int(pStmt, 6);
......
......@@ -48,6 +48,7 @@ bool Game::Initialize() {
deckManager.LoadLFList();
driver = device->getVideoDriver();
driver->setTextureCreationFlag(irr::video::ETCF_CREATE_MIP_MAPS, false);
driver->setTextureCreationFlag(irr::video::ETCF_OPTIMIZED_FOR_QUALITY, true);
imageManager.SetDevice(device);
if(!imageManager.Initial())
return false;
......
......@@ -31,27 +31,27 @@ bool MenuHandler::OnEvent(const irr::SEvent& event) {
break;
}
case BUTTON_JOIN_HOST: {
struct addrinfo hints, *servinfo;
memset(&hints, 0, sizeof(struct addrinfo));
hints.ai_family = AF_INET; /* Allow IPv4 or IPv6 */
hints.ai_socktype = SOCK_STREAM; /* Datagram socket */
hints.ai_flags = AI_PASSIVE; /* For wildcard IP address */
hints.ai_protocol = 0; /* Any protocol */
hints.ai_canonname = NULL;
hints.ai_addr = NULL;
hints.ai_next = NULL;
// struct addrinfo hints, *servinfo;
// memset(&hints, 0, sizeof(struct addrinfo));
// hints.ai_family = AF_INET; /* Allow IPv4 or IPv6 */
// hints.ai_socktype = SOCK_STREAM; /* Datagram socket */
// hints.ai_flags = AI_PASSIVE; /* For wildcard IP address */
// hints.ai_protocol = 0; /* Any protocol */
// hints.ai_canonname = NULL;
// hints.ai_addr = NULL;
// hints.ai_next = NULL;
int status;
char hostname[100];
char ip[20];
const wchar_t* pstr = mainGame->ebJoinIP->getText();
BufferIO::CopyWStr(pstr, hostname, 100);
if ((status = getaddrinfo(hostname, NULL, &hints, &servinfo)) == -1) {
//fprintf(stderr, "getaddrinfo error: %s\n", gai_strerror(status));
//error handling
// if ((status = getaddrinfo(hostname, NULL, &hints, &servinfo)) == -1) {
// //fprintf(stderr, "getaddrinfo error: %s\n", gai_strerror(status));
// //error handling
BufferIO::CopyWStr(pstr, ip, 16);
} else
inet_ntop(AF_INET, &(((struct sockaddr_in *)servinfo->ai_addr)->sin_addr), ip, 20);
freeaddrinfo(servinfo);
// } else
// inet_ntop(AF_INET, &(((struct sockaddr_in *)servinfo->ai_addr)->sin_addr), ip, 20);
// freeaddrinfo(servinfo);
unsigned int remote_addr = htonl(inet_addr(ip));
unsigned int remote_port = _wtoi(mainGame->ebJoinPort->getText());
BufferIO::CopyWStr(pstr, mainGame->gameConf.lastip, 20);
......
// Copyright (C) 2002-2010 Nikolaus Gebhardt
// This file is part of the "Irrlicht Engine".
// For conditions of distribution and use, see copyright notice in irrlicht.h
#ifndef __IRR_OS_H_INCLUDED__
#define __IRR_OS_H_INCLUDED__
#include "IrrCompileConfig.h" // for endian check
#include "irrTypes.h"
#include "irrString.h"
#include "path.h"
#include "ILogger.h"
namespace irr
{
namespace os
{
class Byteswap
{
public:
static u16 byteswap(u16 num);
static s16 byteswap(s16 num);
static u32 byteswap(u32 num);
static s32 byteswap(s32 num);
static f32 byteswap(f32 num);
// prevent accidental swapping of chars
static u8 byteswap(u8 num);
static c8 byteswap(c8 num);
};
class Printer
{
public:
// prints out a string to the console out stdout or debug log or whatever
static void print(const c8* message);
static void log(const c8* message, ELOG_LEVEL ll = ELL_INFORMATION);
static void log(const wchar_t* message, ELOG_LEVEL ll = ELL_INFORMATION);
static void log(const c8* message, const c8* hint, ELOG_LEVEL ll = ELL_INFORMATION);
static void log(const c8* message, const io::path& hint, ELOG_LEVEL ll = ELL_INFORMATION);
static ILogger* Logger;
};
class Randomizer
{
public:
//! resets the randomizer
static void reset();
//! generates a pseudo random number
static s32 rand();
private:
static s32 seed;
};
class Timer
{
public:
//! returns the current time in milliseconds
static u32 getTime();
//! initializes the real timer
static void initTimer();
//! sets the current virtual (game) time
static void setTime(u32 time);
//! stops the virtual (game) timer
static void stopTimer();
//! starts the game timer
static void startTimer();
//! sets the speed of the virtual timer
static void setSpeed(f32 speed);
//! gets the speed of the virtual timer
static f32 getSpeed();
//! returns if the timer currently is stopped
static bool isStopped();
//! makes the virtual timer update the time value based on the real time
static void tick();
//! returns the current real time in milliseconds
static u32 getRealTime();
private:
static void initVirtualTimer();
static f32 VirtualTimerSpeed;
static s32 VirtualTimerStopCounter;
static u32 StartRealTime;
static u32 LastVirtualTime;
static u32 StaticTime;
};
} // end namespace os
} // end namespace irr
#endif
......@@ -21,7 +21,7 @@ class group;
struct card_data {
uint32 code;
uint32 alias;
uint32 setcode;
uint64 setcode;
uint32 type;
uint32 level;
uint32 attribute;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment