-
Notifications
You must be signed in to change notification settings - Fork 1
/
ImageWidget.cpp
125 lines (105 loc) · 4.87 KB
/
ImageWidget.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
///////////////////////////////////////////////////////////////////////////////
//
// ImageWidget.cpp Author: Stephen Chenney
// Modified: Eric McDaniel
// Date: Spring 2002
// Modified: Feng Liu
// Date: Winter 2011
// Why: Change the library file
// Implementation of ImageWidget methods. You do not need to modify this
// file.
//
///////////////////////////////////////////////////////////////////////////////
#include "Globals.h"
#include "ImageWidget.h"
#include <FL/Fl_Window.H>
#include <FL/Fl_Input.H>
#include <FL/Fl_Box.H>
#include <FL/fl_draw.H>
#include "libtarga.h"
#include <string.h>
#include "TargaImage.h"
#include "ScriptHandler.h"
// constants
const int c_border = 10; // border width between window elements in pixels
const int c_buttonHeight = 30; // button height in pixels
const int c_commandInputBoxWidth = 200; // input box width
const int c_commandTextWidth = 120; // static text width
const int c_buttonPaneHeight = 2 * c_border + c_buttonHeight; // height of pane for buttons in pixels
const int c_minWindowWidth = 350; // minimum window width in pixels
const int c_minWindowHeight = 100; // minimum windoe height in pixels
///////////////////////////////////////////////////////////////////////////////
//
// Constructor. Add the buttons to the window.
//
///////////////////////////////////////////////////////////////////////////////
ImageWidget::ImageWidget(int x, int y, int w, int h, char *title) : Fl_Widget(x, y, Max(w, c_minWindowWidth), Max(h, c_minWindowHeight), title), m_pImage(NULL)
{
// add controls-
int horizontalCenter = Max(w, c_minWindowWidth) / 2;
int halfControlWidth = (c_commandInputBoxWidth + c_commandTextWidth + c_border) / 2;
int verticalButtonPos = c_border;
// add label
m_pStaticTextBox = new Fl_Box(horizontalCenter - halfControlWidth, verticalButtonPos, c_commandTextWidth, c_buttonHeight, "Enter Command:");
// add input box
m_pCommandInput = new Fl_Input(horizontalCenter - halfControlWidth + c_commandTextWidth, verticalButtonPos, c_commandInputBoxWidth, c_buttonHeight, "");
m_pCommandInput->callback(CommandCallback, this);
m_pCommandInput->when(FL_WHEN_ENTER_KEY|FL_WHEN_NOT_CHANGED);
}// ImageWidget
///////////////////////////////////////////////////////////////////////////////
//
// Destructor. Free targa imaeg object.
//
///////////////////////////////////////////////////////////////////////////////
ImageWidget::~ImageWidget()
{
delete m_pImage;
}// ~ImageWidget
///////////////////////////////////////////////////////////////////////////////
//
// Get the current image.
//
///////////////////////////////////////////////////////////////////////////////
TargaImage* ImageWidget::Get_Image()
{
return m_pImage;
}// Get_Image
///////////////////////////////////////////////////////////////////////////////
//
// Draw the window contents.
//
///////////////////////////////////////////////////////////////////////////////
void ImageWidget::draw()
{
if (!m_pImage) // Don't do anything if the image is empty.
return;
unsigned char *rgb;
rgb = m_pImage->To_RGB(); // Convert the pre-multiplied RGBA image into RGB.
unsigned int imageX = x() + (w() > m_pImage->width) ? (w() - m_pImage->width) / 2 : 0;
fl_draw_image(rgb, imageX, y() + c_border * 2 + c_buttonHeight, m_pImage->width, m_pImage->height, 3);
delete[] rgb;
}// draw
///////////////////////////////////////////////////////////////////////////////
//
// Redraw the window.
//
///////////////////////////////////////////////////////////////////////////////
void ImageWidget::Redraw()
{
if (m_pImage)
parent()->size(Max(m_pImage->width, c_minWindowWidth), Max(m_pImage->height + c_buttonPaneHeight, c_minWindowHeight));
else
parent()->size(c_minWindowWidth, c_minWindowHeight);
parent()->redraw();
}// Redraw
///////////////////////////////////////////////////////////////////////////////
//
// Handle commands entered in input box.
//
///////////////////////////////////////////////////////////////////////////////
void ImageWidget::CommandCallback(Fl_Widget* pWidget, void* pData)
{
ImageWidget* pImageWidget = static_cast<ImageWidget*>(pData);
CScriptHandler::HandleCommand(static_cast<Fl_Input*>(pWidget)->value(), pImageWidget->m_pImage);
pImageWidget->Redraw();
}// CommandCallback