LiveFunctionWidget::LiveFunctionWidget(Vector2n Position, TypingModule & TypingModule, Project & Project)
	: CompositeWidget(Position, {
		std::shared_ptr<Widget>(m_ToggleWidget = new ToggleWidget(Vector2n(-1, -18), [=](bool State) { m_LiveProgramWidget->m_SourceWidget->m_Visible = State; }, true)),
		std::shared_ptr<Widget>(new FlowLayoutWidget(Vector2n::ZERO, { std::shared_ptr<Widget>(m_InputWidget = new TextFieldWidget(Vector2n::ZERO, TypingModule)),
								   std::shared_ptr<Widget>(m_SourceWidget = new TextFieldWidget(Vector2n::ZERO, TypingModule)),
								   std::shared_ptr<Widget>(m_LiveProgramWidget = new LiveProgramWidget(Vector2n::ZERO, TypingModule, Project)) }, { }))

	}, { std::shared_ptr<Behavior>(new DraggablePositionBehavior(*this)) })
{
	m_LiveProgramWidget->RemoveAllBehaviors();

	// With live test execution and all
	m_SourceWidget->m_OnChange = [=, &Project]()
	{
		std::ostringstream GenProgram;
		Project.GenerateProgramForFunction(GenProgram, m_InputWidget->GetContent(), m_SourceWidget->GetContent());
		m_LiveProgramWidget->m_SourceWidget->SetContent(GenProgram.str());
	};

	m_InputWidget->m_OnChange = m_SourceWidget->m_OnChange;

	{
		auto ImportList = new ListWidget<ConceptId>(Vector2n::ZERO, Project.GetStdIncludes(), TypingModule);
		auto pTypingModule = &TypingModule;
		ImportList->m_TapAction = [=](Vector2n LocalPosition, std::vector<ConceptId> & m_List)
		{
			auto Entry = pTypingModule->TakeString();

			if (!Entry.empty())
			{
				auto ConceptId = FindOrCreateConcept(Entry);

				//Insert(ConceptId);

				// TEST
				auto Spot = m_List.begin() + (LocalPosition.Y() / lineHeight);
				m_List.insert(Spot, ConceptId);
			}
			else
			{
				auto ListEntry = static_cast<decltype(m_List.size())>(LocalPosition.Y() / lineHeight);

				if (ListEntry < m_List.size())
				{
					pTypingModule->SetString(GetConcept(m_List[ListEntry]).GetContent());
					m_List.erase(m_List.begin() + ListEntry);
				}
			}
		};

		auto LabelledImportList = new FlowLayoutWidget(Vector2n(-280, -100), { std::shared_ptr<Widget>(new LabelWidget(Vector2n::ZERO, std::string("import (\""), LabelWidget::Background::None)),
																			   std::shared_ptr<Widget>(ImportList),
																			   std::shared_ptr<Widget>(new LabelWidget(Vector2n::ZERO, std::string("\")"), LabelWidget::Background::None)) }, {});
		static_cast<FlowLayoutWidget *>(GetWidgets()[1].get())->AddWidget(LabelledImportList);
	}
}
void ConceptStringBoxWidget::ProcessEvent(InputEvent & InputEvent)
{
	if (InputEvent.HasType(InputEvent::EventType::BUTTON_EVENT))
	{
		auto ButtonId = InputEvent.m_InputId;
		bool Pressed = InputEvent.m_Buttons[0];		// TODO: Check if there are >1 buttons

		if (Pointer::VirtualCategory::TYPING == InputEvent.m_Pointer->GetVirtualCategory())
		{
			if (Pressed)
			{
				bool HandledEvent = true;		// Assume true at first

				switch (ButtonId)
				{
				case GLFW_KEY_BACKSPACE:
					/*{
						// Erase the last concept
						if (false == m_Content.empty())
						{
							m_Content.pop_back();
						}

						InputEvent.m_Handled = true;
					}*/
					break;
				case GLFW_KEY_ENTER:
					{
					}
					break;
				case GLFW_KEY_TAB:
					{
					}
					break;
				case GLFW_KEY_LEFT:
					{
						MoveCaretTry(-1, true);
					}
					break;
				case GLFW_KEY_RIGHT:
					{
						MoveCaretTry(+1, true);
					}
					break;
				default:
					HandledEvent = false;
					break;
				}

				if (HandledEvent)
				{
					InputEvent.m_Handled = true;
				}
			}
		}
		else if (Pointer::VirtualCategory::POINTING == InputEvent.m_Pointer->GetVirtualCategory())
		{
			if (HasTypingFocus())
			{
				if (Pressed)
				{
					bool HandledEvent = true;		// Assume true at first

					switch (ButtonId)
					{
					case 0:
						{
							auto Entry = m_TypingModule.TakeString();

							if (!Entry.empty())
							{
								auto ConceptId = FindOrCreateConcept(Entry);

								m_Content.push_back(ConceptId);
							}
							else
							{
								if (!m_Content.empty())
								{
									if (m_CaretPosition >= m_Content.size())
									{
										MoveCaretTry(-1, true);
									}

									m_TypingModule.SetString(GetConcept(m_Content.back()).GetContent());
									m_Content.pop_back();
								}
							}
						}
						break;
					default:
						HandledEvent = false;
						break;
					}

					if (HandledEvent)
					{
						InputEvent.m_Handled = true;
					}
				}
			}
		}
	}
}
예제 #3
0
ConceptionApp::ConceptionApp(InputManager & InputManager)
    : App(InputManager),
      m_CurrentProject(),
      m_TypingModule(new TypingModule())		// Gets cleaned up via unique_ptr when pushed back to m_Widgets
{
    PopulateConcepts();

    {
        auto MainCanvas = new Canvas(Vector2n(0, 0), true, true);
        //MainCanvas->MoveView(0, 336);
        MainCanvas->MoveView(1, -64);

#if 1
        {
            auto StdIncludesList = new ListWidget<ConceptId>(Vector2n::ZERO, m_CurrentProject.GetStdIncludes(), *m_TypingModule);
            StdIncludesList->m_TapAction = [=](Vector2n LocalPosition, std::vector<ConceptId> & m_List)
            {
                auto Entry = m_TypingModule->TakeString();

                if (!Entry.empty())
                {
                    auto ConceptId = FindOrCreateConcept(Entry);

                    //Insert(ConceptId);

                    // TEST
                    auto Spot = m_List.begin() + (LocalPosition.Y() / lineHeight);
                    m_List.insert(Spot, ConceptId);
                }
                else
                {
                    auto ListEntry = static_cast<decltype(m_List.size())>(LocalPosition.Y() / lineHeight);

                    if (ListEntry < m_List.size())
                    {
                        m_TypingModule->SetString(GetConcept(m_List[ListEntry]).GetContent());
                        m_List.erase(m_List.begin() + ListEntry);
                    }
                }
            };

            auto LabelledStdIncludesList = new FlowLayoutWidget(Vector2n(-280, -250), { std::shared_ptr<Widget>(new LabelWidget(Vector2n::ZERO, std::string("#include <"), LabelWidget::Background::None)),
                    std::shared_ptr<Widget>(StdIncludesList),
                    std::shared_ptr<Widget>(new LabelWidget(Vector2n::ZERO, std::string(">"), LabelWidget::Background::None))
                                                                                      }, {});
            LabelledStdIncludesList->AddBehavior(std::shared_ptr<Behavior>(new DraggablePositionBehavior(*LabelledStdIncludesList)));
            MainCanvas->AddWidget(LabelledStdIncludesList);
        }
#endif

        MainCanvas->AddWidget(new ButtonWidget(Vector2n(-100, -350), []() {
            std::cout << "Hi from anon func.\n";
        } ));
        MainCanvas->AddWidget(new ButtonWidget(Vector2n(-60, -350), []() {
            std::cout << "Second button.\n";
        } ));
        MainCanvas->AddWidget(new ToggleWidget(Vector2n(-20, -350), [](bool State) {
            std::cout << "Testing this toggle widget! It's now set to " << State << ".\n";
        }, true));
        MainCanvas->AddWidget(new LiveFunctionWidget(Vector2n(-100, 100), *m_TypingModule, m_CurrentProject));
        MainCanvas->AddWidget(new LiveProgramWidget(Vector2n(-100, -300), *m_TypingModule, m_CurrentProject));
        MainCanvas->AddWidget(new LiveProgramWidget(Vector2n(-100, -100), *m_TypingModule, m_CurrentProject));
        MainCanvas->AddWidget(new LiveGofmtWidget(Vector2n(-460, 200), *m_TypingModule, m_CurrentProject));
        MainCanvas->AddWidget(new TextFieldWidget(Vector2n(-460, 160), *m_TypingModule));
        MainCanvas->AddWidget(new ShellWidget(Vector2n(-460, 60), *m_TypingModule));
        MainCanvas->AddWidget(new SayWidget(Vector2n(-460, -100), *m_TypingModule));

        MainCanvas->AddWidget(new ConceptStringBoxWidget(Vector2n(-400, 100 + 400), *m_TypingModule));

        // TEST: Modify some Concept
        {
            auto Widget = new TextFieldWidget(Vector2n(-320, 470), *m_TypingModule);
            Widget->SetContent(GetConcept(47).GetContent());
            Widget->m_OnChange = [=]() {
                static_cast<ConceptBasic &>(ModifyConcept(47)).SetContentTEST(Widget->GetContent());
            };
            Widget->AddBehavior(std::shared_ptr<Behavior>(new DraggablePositionBehavior(*Widget)));
            MainCanvas->AddWidget(Widget);
        }

        // Label resizing test
        {
            auto SourceWidget = new TextFieldWidget(Vector2n::ZERO, *m_TypingModule);

            auto Content = [=]() -> std::string {
                return SourceWidget->GetContent();
            };
            auto LabelWidget = new class LabelWidget(Vector2n::ZERO, Content, LabelWidget::Background::Normal);

            MainCanvas->AddWidget(new FlowLayoutWidget(Vector2n(-100, -450), { std::shared_ptr<Widget>(SourceWidget), std::shared_ptr<Widget>(LabelWidget) }, {}));
        }

        // Time widget
        {
            auto Content = []() -> std::string {
                auto now = std::chrono::system_clock::now();

                auto duration = now.time_since_epoch();

                auto seconds = std::chrono::duration_cast<std::chrono::seconds>(duration).count();

                return std::to_string(seconds);
            };
            auto LabelWidget = new class LabelWidget(Vector2n(360, -340), Content, LabelWidget::Background::Normal);
            LabelWidget->AddBehavior(std::shared_ptr<Behavior>(new DraggablePositionBehavior(*LabelWidget)));

            MainCanvas->AddWidget(LabelWidget);
        }

        MainCanvas->AddWidget(new TimeWidget(Vector2n(360, -360)));		// Time widget

#if 0
        // "./GenProgram.go" file contents displayed (in real-time) in this Label Widget
        {
            auto Content = []() -> std::string {
                //return FromFileToString("./GenProgram.go");
                return FromFileToString("/Users/Dmitri/Desktop/goproj_play/src/gist.github.com/4670289.git/gistfile1.go");
            };
            auto LabelWidget = new class LabelWidget(Vector2n(-546, -186), Content, LabelWidget::Background::Normal);
            LabelWidget->AddBehavior(std::shared_ptr<Behavior>(new DraggablePositionBehavior(*LabelWidget)));

            MainCanvas->AddWidget(LabelWidget);
        }
#endif

#if 1
        {
            MainCanvas->AddWidget(new ListWidget<Concept *>(Vector2n(-730 - 450, -250), Concepts, *m_TypingModule));
        }
#endif

        m_Widgets.push_back(std::unique_ptr<Widget>(m_TypingModule));

        m_Widgets.push_back(std::unique_ptr<Widget>(MainCanvas));

        m_Widgets.push_back(std::unique_ptr<Widget>(new DebugOverlayWidget()));		// DEBUG: Print debug info
    }

    // Prepare and start the thread
    {
        m_CurrentProject.StartBackgroundThread();
    }

    {
        // Load program
        m_CurrentProject.LoadSampleGenProgram(*static_cast<Canvas *>(m_Widgets[0].get()));
    }
}