Ejemplo n.º 1
0
float px::engine::utils::scaling::pixelToPoint()
{
    static float scale = -1.0f;
    
    if (scale < 0.0)
    {
        scale = 1.0f / pointToPixel();
        PXLLOG("ScaleUtils::pixelToPoint() : %.2f", scale);
    }
	
    return scale;
}
Ejemplo n.º 2
0
int Theme::GetTextFontSize() const
{
    if (textFontSize==0) {
#ifdef __ANDROID__
        textFontSize=(int)mmToPixel(2.0);
#else
        QFont font;

        textFontSize=font.pixelSize();

        if (textFontSize==-1) {
            textFontSize=pointToPixel(font.pointSize());
        }

#endif
        qDebug() << "TextFontSize:" << textFontSize << "px";
    }

    return textFontSize;
}
void QSysMLBlock::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{
	QColor color = qdiagramproperty_cast<QColor>(property("backgroundColor"));
    painter->setBrush(QBrush(color, Qt::SolidPattern));
    painter->drawRect(0, 0, geometry().width(), geometry().height());

    QFont f = pointToPixel(qdiagramproperty_cast<QFont>(property("textFont")));
 
    QFontMetricsF fm(f);
    qreal pixelsWide = fm.width("M");
    qreal pixelsHigh = fm.height();
    // « »
    QRect br = boundingRect().adjusted(-2, -2, -2, -2).toRect();
    QRect r(0, 5, geometry().width(), pixelsHigh);
    painter->setFont(f);
    painter->drawText(r, Qt::AlignHCenter, "«block»");
    f.setBold(true);
    painter->setFont(f);
    painter->drawText(0, r.height() + fm.leading(), geometry().width(), geometry().height(), Qt::AlignHCenter | Qt::TextWordWrap, property("name").toString(), &br);

    paintFocus(painter, option, widget);
}
Ejemplo n.º 4
0
void updateEditor(SDL_Event event, EditorData* data)
{
	
	GUI_sendEventToGUI(data->gui, &event);

	const unsigned char* keyboardState = SDL_GetKeyboardState(NULL);
	int2 mousePixel;
	SDL_GetMouseState(&mousePixel.x, &mousePixel.y);

	float2 mousePos = pixelToPoint(&data->view, mousePixel);


	updateBones(data);

	switch (event.type)
	{
	case SDL_KEYDOWN:
	{

		switch (event.key.keysym.scancode)
		{
		case SDL_SCANCODE_S:
			if (event.key.keysym.mod == KMOD_LCTRL)
			{
				SDL_Event lastEvent = data->lowEventStack.Top();

				if (lastEvent.key.keysym.scancode == SDL_SCANCODE_S && lastEvent.key.keysym.mod == KMOD_LCTRL)
				{
					// save skeleton

					GUI_WidgetID widget = GUI_addContainer(data->gui, GUI_MAIN_WIDGET, { 400 - 100, 300 - 25, 200, 50}, GUI_Container::Orientation::HORIZONTAL);

					auto callbackSave = [](GUI* gui, GUI_WidgetID widgetID, void* editorData)
					{
						EditorData* data = (EditorData*)editorData;

						GUI_WidgetID widget = GUI_getParentWidget(gui, widgetID);

						const char* filename = GUI_getTextfield(gui, GUI_getChildWidget(gui, widget, 0)).text;

						saveSkeleton(filename, &data->skeleton[0], &data->names[0], data->skeleton.size());

						GUI_removeWidget(gui, widget);
					};

					auto callbackCancel = [](GUI* gui, GUI_WidgetID widgetID, void*)
					{
						GUI_WidgetID widget = GUI_getParentWidget(gui, widgetID);
						GUI_removeWidget(gui, widget);
					};

					GUI_addTextfield(data->gui, widget, { 0, 0, -1, -1 }, "filename");
					GUI_addButton(data->gui, widget, { 0, 0, 50, -1 }, "Save", data, callbackSave);
					GUI_addButton(data->gui, widget, { 0, 0, 50, -1 }, "Cancel", 0, callbackCancel);

					break;
				}
				else if (lastEvent.key.keysym.scancode == SDL_SCANCODE_O && lastEvent.key.keysym.mod == KMOD_LCTRL)
				{
					// load skeleton

					GUI_WidgetID widget = GUI_addContainer(data->gui, GUI_MAIN_WIDGET, { 400 - 100, 300 - 15, 200, 30 }, GUI_Container::Orientation::HORIZONTAL);


					auto callbackLoad = [](GUI* gui, GUI_WidgetID widgetID, void* editorData)
					{
						EditorData* data = (EditorData*)editorData;

						GUI_WidgetID widget = GUI_getParentWidget(gui, widgetID);

						const char* filename = GUI_getTextfield(gui, GUI_getChildWidget(gui, widget, 0)).text;

						strcpy(data->skeletonName, filename);
						
						const SkeletonSave* save = loadSkeleton(filename);

						if (save != 0)
						{
							data->bones.clear();
							data->skeleton.clear();
							data->constraints.clear();
							data->names.clear();

							const char* name = save->names;
							for (int i = 0; i < save->numBones; ++i)
							{
								Bone newBone;
								newBone.jointAngle = { 1, 0 };
								newBone.jointPos = save->jointPos[i];
								newBone.parentJoint = save->parentJoints[i];
								newBone.name = data->names.push({ { 0 } });

								memcpy(data->names[newBone.name].string, name, save->nameLen[i] * sizeof(char));
								name += save->nameLen[i];

								Constraint constraint;
								constraint.minAngle = -PI;
								constraint.maxAngle = 3 * PI;

								data->skeleton.push(newBone);
								data->constraints.push(constraint);
							}

							updateBones(data);
						}

						GUI_removeWidget(gui, widget);
					};

					auto callbackCancel = [](GUI* gui, GUI_WidgetID widgetID, void*)
					{
						GUI_WidgetID widget = GUI_getParentWidget(gui, widgetID);
						GUI_removeWidget(gui, widget);
					};

					GUI_addTextfield(data->gui, widget, { 0, 0, -1, -1 }, "filename");
					GUI_addButton(data->gui, widget, { 0, 0, 50, -1 }, "Load", data, callbackLoad);
					GUI_addButton(data->gui, widget, { 0, 0, 50, -1 }, "Cancel", 0, callbackCancel);



					break;
				}


				data->lowEventStack.Push(event);
			}


			break;
			case SDL_SCANCODE_K:
				if (event.key.keysym.mod == KMOD_LCTRL)
				{
					SDL_Event lastEvent = data->lowEventStack.Top();

					if (lastEvent.key.keysym.scancode == SDL_SCANCODE_S && lastEvent.key.keysym.mod == KMOD_LCTRL)
					{
						// save keyFrame

						GUI_WidgetID widget = GUI_addContainer(data->gui, GUI_MAIN_WIDGET, { 400 - 100, 300 - 15, 200, 30 }, GUI_Container::Orientation::HORIZONTAL);

						auto callbackSave = [](GUI* gui, GUI_WidgetID widgetID, void* editorData)
						{
							EditorData* data = (EditorData*)editorData;

							GUI_WidgetID widget = GUI_getParentWidget(gui, widgetID);

							const char* filename = GUI_getTextfield(gui, GUI_getChildWidget(gui, widget, 0)).text;

							saveKeyFrame(filename, &data->skeleton[0], &data->names[0], data->skeleton.size());

							GUI_removeWidget(gui, widget);
						};

						auto callbackCancel = [](GUI* gui, GUI_WidgetID widgetID, void*)
						{
							GUI_WidgetID widget = GUI_getParentWidget(gui, widgetID);
							GUI_removeWidget(gui, widget);
						};

						GUI_addTextfield(data->gui, widget, { 0, 0, -1, -1 }, "filename");
						GUI_addButton(data->gui, widget, { 0, 0, 50, -1 }, "Save", data, callbackSave);
						GUI_addButton(data->gui, widget, { 0, 0, 50, -1 }, "Cancel", 0, callbackCancel);

						break;
					}
					else if (lastEvent.key.keysym.scancode == SDL_SCANCODE_O && lastEvent.key.keysym.mod == KMOD_LCTRL)
					{
						// load keyframe

						GUI_WidgetID widget = GUI_addContainer(data->gui, GUI_MAIN_WIDGET, { 400 - 100, 300 - 15, 200, 30 }, GUI_Container::Orientation::HORIZONTAL);


						auto callbackLoad = [](GUI* gui, GUI_WidgetID widgetID, void* editorData)
						{
							EditorData* data = (EditorData*)editorData;

							GUI_WidgetID widget = GUI_getParentWidget(gui, widgetID);

							const char* filename = GUI_getTextfield(gui, GUI_getChildWidget(gui, widget, 0)).text;

							strcpy(data->frameName, filename);

							const KeyFrameSave* save = loadKeyFrame(filename);

							if (save != 0)
							{

								const char* name = save->names;
								for (int i = 0; i < save->numBones; ++i)
								{
									int nameID = -1;
									for (int j = 0; j < data->names.size(); ++j)
									{
										if (strncmp(name, data->names[j].string, save->nameLen[i]) == 0)
										{
											nameID = j;
											break;
										}
									}

									if (nameID != -1)
									{

										if (data->skeleton[nameID].name == nameID)
										{
											data->skeleton[nameID].jointAngle = save->jointAngles[i];
										}
										else
										{
											for (int j = 0; j < data->skeleton.size(); j++)
											{
												if (data->skeleton[j].name = nameID)
													data->skeleton[j].jointAngle = save->jointAngles[i];
											}
										}
									}

									name += save->nameLen[i];
								}

								updateBones(data);
							}

							GUI_removeWidget(gui, widget);
						};

						auto callbackCancel = [](GUI* gui, GUI_WidgetID widgetID, void*)
						{
							GUI_WidgetID widget = GUI_getParentWidget(gui, widgetID);
							GUI_removeWidget(gui, widget);
						};

						GUI_addTextfield(data->gui, widget, { 0, 0, -1, -1 }, "filename");
						GUI_addButton(data->gui, widget, { 0, 0, 50, -1 }, "Load", data, callbackLoad);
						GUI_addButton(data->gui, widget, { 0, 0, 50, -1 }, "Cancel", 0, callbackCancel);


						break;
					}


					data->lowEventStack.Push(event);
				}


				break;
		default:
			data->lowEventStack.Push(event);
			break;

		}
		break;
	}
	case SDL_MOUSEBUTTONDOWN:
	{
		data->newBone = -1;

		if (keyboardState[SDL_SCANCODE_A])
		{
			for (int i = 0; i < data->bones.size(); ++i)
			{
				if (pointInRect(mousePos, data->bones[i].rect))
				{
					int name = data->names.push({ "test" });

					Bone newBone;
					newBone.jointAngle = { 1, 0 };
					newBone.jointPos = { 0, 0 };
					newBone.parentJoint = i;
					newBone.name = name;

					Constraint constraint;
					constraint.minAngle = -PI;
					constraint.maxAngle = 3 * PI;

					int newBoneID = data->skeleton.push(newBone);
					data->constraints.push(constraint);

					int* ids = new int[data->skeleton.size()];

					sortSkeleton(&data->skeleton[0], data->skeleton.size(), ids);

					Constraint* tempConstraints = new Constraint[data->constraints.size()];
					memcpy(tempConstraints, &data->constraints[0], sizeof(Constraint) * data->constraints.size());

					for (int i = 0; i < data->constraints.size(); ++i)
					{
						data->constraints[i] = tempConstraints[ids[i]];
					}

					delete[] tempConstraints;

					//find bone again
					data->newBone = ids[newBoneID];

					// rebuild bone selection

					updateBones(data);

					delete[]ids;


					break;
				}



			}
		}
		else if (keyboardState[SDL_SCANCODE_N])
		{

			for (int i = 0; i < data->bones.size(); ++i)
			{
				if (pointInRect(mousePos, data->bones[i].rect))
				{
					
					int2 pos = pointToPixel(&data->view, float2{ data->bones[i].rect.x + 10, data->bones[i].rect.y });
					
					GUI_WidgetID widget = GUI_addContainer(data->gui, GUI_MAIN_WIDGET, intRect{ pos.x, pos.y, 100, 30 }, GUI_Container::HORIZONTAL);

					struct Args
					{
						EditorData* data;
						int boneId;
					}*args = new Args{ data, i };
						

					auto callbackOk = [](GUI* gui, GUI_WidgetID widgetID, void* args)
					{
						EditorData* data = ((Args*)args)->data;
						int boneID = ((Args*)args)->boneId;

						GUI_WidgetID parentID = GUI_getParentWidget(gui, widgetID);

						const char* newName = GUI_getTextfield(gui, GUI_getChildWidget(gui, parentID, 0)).text;

						strcpy(data->names[data->skeleton[boneID].name].string, newName);

						GUI_removeWidget(gui, parentID);

						delete args;
					};

					GUI_addTextfield(data->gui, widget, intRect{ 0, 0, -1, -1 }, data->names[data->skeleton[i].name].string);
					GUI_addButton(data->gui, widget, intRect{ 0, 0, 30, -1 }, "OK", args, callbackOk);

					break;
				}
			}
		}
		else
		{



			for (int i = 0; i < data->bones.size(); i++)
			{
				if (pointInRect(mousePos, data->bones[i].rect))
				{
					data->selectedBones.push(i);
					data->grabbedBone = i;
				}
			}

			if (data->grabbedBone == -1 && !keyboardState[SDL_SCANCODE_LCTRL])
				data->selectedBones.clear();
		}

		break;
	}
	case SDL_MOUSEBUTTONUP:
	{

		data->grabbedBone = -1;
		break;
	}
	case SDL_MOUSEMOTION:
	{

		

		if (SDL_GetMouseState(0, 0) == SDL_BUTTON_MIDDLE)
		{
			data->view.position.x -= (event.motion.xrel / data->view.scale.x);
			data->view.position.y -= (event.motion.yrel / data->view.scale.y);

		}
		else
		{

			float2 motion = rotate({ (float)event.motion.xrel, (float)event.motion.yrel }, data->view.angle) * float2{ 1 / data->view.scale.x, 1 / data->view.scale.y };

			if (data->newBone != -1)
			{
				float2 angle = getBoneWorldTransform(&data->skeleton[0], data->skeleton.size(), data->newBone).angle;

				float2 rel = rotate(float2{ motion.x, motion.y }, negateRotation(angle));

				data->skeleton[data->newBone].jointPos += rel;
			}


			if (data->grabbedBone != -1)
			{
				if (keyboardState[SDL_SCANCODE_LCTRL])
				{
					float2 angle = getBoneWorldTransform(&data->skeleton[0], data->skeleton.size(), data->skeleton[data->grabbedBone].parentJoint).angle;

					float2 rel = rotate(float2{ motion.x, motion.y }, negateRotation(angle));

					data->skeleton[data->grabbedBone].jointPos += rel;
				}
				else
				{
					animateCCDIKSelection(&data->skeleton[0], &data->constraints[0], data->skeleton.size(),
						&data->selectedBones[0], data->selectedBones.size(), data->grabbedBone, mousePos - float2{ 400, 300 });
				}
			}

		}

		break;
	}
	case SDL_MOUSEWHEEL:
	{
		data->view.scale.x += event.wheel.y*0.1f;
		data->view.scale.y += event.wheel.y*0.1f;

		break;
	}
	}


	
}