Skip to content

Commit

Permalink
New Find/Jump to Item Dialog, some code cleanup.
Browse files Browse the repository at this point in the history
  • Loading branch information
Mignari committed May 19, 2017
1 parent fe81567 commit 63aebaf
Show file tree
Hide file tree
Showing 18 changed files with 772 additions and 332 deletions.
2 changes: 2 additions & 0 deletions source/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ ${CMAKE_CURRENT_LIST_DIR}/editor.h
${CMAKE_CURRENT_LIST_DIR}/editor_tabs.h
${CMAKE_CURRENT_LIST_DIR}/extension.h
${CMAKE_CURRENT_LIST_DIR}/extension_window.h
${CMAKE_CURRENT_LIST_DIR}/find_item_window.h
${CMAKE_CURRENT_LIST_DIR}/filehandle.h
${CMAKE_CURRENT_LIST_DIR}/graphics.h
${CMAKE_CURRENT_LIST_DIR}/ground_brush.h
Expand Down Expand Up @@ -125,6 +126,7 @@ ${CMAKE_CURRENT_LIST_DIR}/editor_tabs.cpp
${CMAKE_CURRENT_LIST_DIR}/eraser_brush.cpp
${CMAKE_CURRENT_LIST_DIR}/extension.cpp
${CMAKE_CURRENT_LIST_DIR}/extension_window.cpp
${CMAKE_CURRENT_LIST_DIR}/find_item_window.cpp
${CMAKE_CURRENT_LIST_DIR}/filehandle.cpp
${CMAKE_CURRENT_LIST_DIR}/graphics.cpp
${CMAKE_CURRENT_LIST_DIR}/ground_brush.cpp
Expand Down
107 changes: 0 additions & 107 deletions source/common_windows.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -773,113 +773,6 @@ void FindDialog::RefreshContents()
RefreshContentsInternal();
}

// ============================================================================
// Find Item Dialog (Jump to item)

FindItemDialog::FindItemDialog(wxWindow* parent, wxString title) : FindDialog(parent, title)
{
extra_condition = nullptr;
RefreshContents();
}

FindItemDialog::~FindItemDialog()
{
////
}

void FindItemDialog::setCondition(bool condition(const ItemType&)) {
extra_condition = condition;
}

void FindItemDialog::OnClickListInternal(wxCommandEvent& event)
{
Brush* brush = item_list->GetSelectedBrush();
if(brush && brush->isRaw()) {
result_brush = brush;
result_id = brush->asRaw()->getItemID();
EndModal(result_id);
}
}

void FindItemDialog::OnClickOKInternal()
{
// This is kind of stupid as it would fail unless the "Please enter a search string" wasn't there
if(item_list->GetItemCount() > 0) {
if(item_list->GetSelection() == wxNOT_FOUND) {
item_list->SetSelection(0);
}

Brush* brush = item_list->GetSelectedBrush();
if (brush && brush->isRaw()) {
result_brush = brush;
result_id = brush->asRaw()->getItemID();
} else {
// It's either "Please enter a search string" or "No matches"
// Perhaps we can refresh now?
std::string search_string = as_lower_str(nstr(search_field->GetValue()));
bool do_search = (search_string.size() >= 2);

if(do_search) {
for(int id = 0; id <= g_items.getMaxID(); ++id) {
ItemType& it = g_items[id];
if(it.id == 0 || (extra_condition && !extra_condition(it)))
continue;

RAWBrush* raw_brush = it.raw_brush;
if(!raw_brush)
continue;

if(as_lower_str(raw_brush->getName()).find(search_string) == std::string::npos)
continue;

// Found one!
result_brush = raw_brush;
result_id = raw_brush->getItemID();
break;
}
}
}
}
EndModal(result_id);
}

void FindItemDialog::RefreshContentsInternal()
{
item_list->Clear();

std::string search_string = as_lower_str(nstr(search_field->GetValue()));
bool do_search = (search_string.size() >= 2);

if(do_search) {

bool found_search_results = false;

for(int id = 0; id <= g_items.getMaxID(); ++id) {
ItemType& it = g_items[id];
if(it.id == 0 || (extra_condition && !extra_condition(it)))
continue;

RAWBrush* raw_brush = it.raw_brush;
if(!raw_brush)
continue;

if(as_lower_str(raw_brush->getName()).find(search_string) == std::string::npos)
continue;

found_search_results = true;
item_list->AddBrush(raw_brush);
}


if(found_search_results)
item_list->SetSelection(0);
else
item_list->SetNoMatches();

}
item_list->Refresh();
}

// ============================================================================
// Find Brush Dialog (Jump to brush)

Expand Down
20 changes: 0 additions & 20 deletions source/common_windows.h
Original file line number Diff line number Diff line change
Expand Up @@ -240,26 +240,6 @@ class FindBrushDialog : public FindDialog
virtual void OnClickOKInternal();
};

/**
* Select an item dialog
* Find out what item was selected through return value of ShowModal() or getResultID
*/
class FindItemDialog : public FindDialog
{
public:
FindItemDialog(wxWindow* parent, wxString title = "Jump to Item");
virtual ~FindItemDialog();

void setCondition(bool condition(const ItemType&));

virtual void RefreshContentsInternal();
virtual void OnClickListInternal(wxCommandEvent&);
virtual void OnClickOKInternal();

protected:
bool (*extra_condition)(const ItemType&);
};

/**
* Select two items
* Will return the two selected items through GetResultFindID() and GetResultWithID()
Expand Down
36 changes: 14 additions & 22 deletions source/container_properties_window.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

#include "old_properties_window.h"
#include "properties_window.h"
#include "find_item_window.h"
#include "gui.h"
#include "complexitem.h"
#include "map.h"
Expand Down Expand Up @@ -70,32 +71,23 @@ void ContainerItemButton::OnMouseRightRelease(wxMouseEvent& WXUNUSED(event))

void ContainerItemButton::OnAddItem(wxCommandEvent& WXUNUSED(event))
{
FindItemDialog* itemDialog = newd FindItemDialog(GetParent(), "Choose Item to add");
itemDialog->setCondition([](const ItemType& itemType) -> bool {
return itemType.pickupable;
});
FindItemDialog dialog(GetParent(), "Choose Item to add", true);

int32_t id = itemDialog->ShowModal();
itemDialog->Destroy();
if(dialog.ShowModal() != 0) {
Container* container = getParentContainer();
ItemVector& itemVector = container->getVector();

if(id == 0) {
return;
}

Container* container = getParentContainer();
ItemVector& itemVector = container->getVector();
Item* item = Item::Create(dialog.getResultID());
if(index < itemVector.size())
itemVector.insert(itemVector.begin() + index, item);
else
itemVector.push_back(item);

Item* item = Item::Create(id);
if(index < itemVector.size()) {
itemVector.insert(itemVector.begin() + index, item);
} else {
itemVector.push_back(item);
}

ObjectPropertiesWindowBase* propertyWindow = getParentContainerWindow();
if(propertyWindow) {
propertyWindow->Update();
ObjectPropertiesWindowBase* propertyWindow = getParentContainerWindow();
if(propertyWindow)
propertyWindow->Update();
}
dialog.Destroy();
}

void ContainerItemButton::OnEditItem(wxCommandEvent& WXUNUSED(event))
Expand Down
Loading

0 comments on commit 63aebaf

Please sign in to comment.